user2964771
user2964771

Reputation: 1

curl with php not giving any results

this is my php code, where it is not giving any result, please let me know if I have made any mistake.

<?php
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,"http://www.my-ajax-site.com     'https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=Official%20Google%20Blogs'");
$res=curl_exec($curl_handle);
echo $res;
curl_close($curl_handle);
$json = json_decode($res, true);
echo $json;
print_r($json);
?>

Upvotes: 0

Views: 166

Answers (3)

Marcos Eusebi
Marcos Eusebi

Reputation: 637

http://www.php.net/manual/en/function.curl-setopt.php http://www.php.net/manual/en/function.curl-init.php

resource curl_init ([ string $url = NULL ] )

string $url = NULL

take a look into the parameter description:

url

If provided, the CURLOPT_URL option will be set to its value. You can manually set this using the curl_setopt() function.

Note: The file protocol is disabled by cURL if open_basedir is set.

So, here is the CURLOPT_URL information:

CURLOPT_URL The URL to fetch. This can also be set when initializing a session with curl_init().

http://en.wikipedia.org/wiki/Uniform_resource_locator#Syntax

Well, the command only accept a URL not multiple onces and there is the problem.

curl_setopt($curl_handle,CURLOPT_URL,"http://www.my-ajax-site.com     'https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=Official%20Google%20Blogs'");
//                                               URL 1                                                   URL 2

So, here we have 2 URLs. The parameter is wrong so it fails.

I don't know what you want to do but if you want to join both sites into one string you can do follow:

// Obtain the first URL data into $ObtainedDataFromSFirstCURL variable.
string $variable = $ObtainedDataFromFirstCURL;
// Obtain the second URL data into $ObtainedDataFromSecondCURL variable.
$variable .= $ObtainedDataFromSecondCURL;
// Do anything whit the variable.
echo $variable;

http://www.php.net/manual/en/language.operators.string.php

I hope it helps someway.

Upvotes: 0

That's because you are passing two URLs

curl_setopt($curl_handle,CURLOPT_URL,"http://www.my-ajax-site.com     'https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=Official%20Google%20Blogs'");
          --------------------------URL1--------------------------     ---------------------------------------URL2-------------------------------------

Pass just one URL , If you want to use multiple urls, Make use of a loop and call this cURL method.

EDIT :

Passing multiple URLs..

<?php
$urls = array('http://example.com','http://example2.com');
foreach($urls as $url)
{
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
$res=curl_exec($curl_handle);
echo $res;
curl_close($curl_handle);
$json = json_decode($res, true);
echo $json;
print_r($json);
}
?>

Upvotes: 1

Alfred Armstrong
Alfred Armstrong

Reputation: 238

In the curl command line you quoted, the -e url argument is setting the HTTP referrer, so that's what you need to mimic in your PHP curl. I found a solution at http://www.electrictoolbox.com/php-curl-http-referer/ which I have adapted for your URLs:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=Official%20Google%20Blogs');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://my-ajax-site.com');
$html = curl_exec($ch);

Upvotes: 0

Related Questions