0

Possible Duplicate:
Php - Debugging Curl

I am using curl to fetch the user data from youtube API The code is

    $url_userInfo =  'https://gdata.youtube.com/feeds/api/users/default?access_token=ya29.AHES6ZS7GMdZf91LbMtoOdhFSFOpTuHHT-t7pSggAp-tS0A;

print_r($url_userInfo);
$ch = curl_init($url_userInfo);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
$content = curl_exec($ch);
curl_close($ch);
print_r( $content);

If I manually visit this url it displays the data in xml form. but there is nothing to print in $content.

Is there any problem with code??

Community
  • 1
  • 1
Muhammad Usman
  • 10,426
  • 22
  • 72
  • 107
  • 1
    Unable to reproduce: *"Notice: Undefined variable: ytAccessToken" - please only provide working examples, e.g. provide testing or demo credentials. – hakre Dec 18 '12 at 10:27
  • now try. it will work for an hour – Muhammad Usman Dec 18 '12 at 10:31
  • 1
    I tried, you just need to troubleshoot it. It's trivial, see the linked question. Please do your homework before posting a new question and always add debug information if you run into a problem. Also please see [What Steps do you Take to Troubleshoot Problems with PHP cURL?](http://stackoverflow.com/questions/815910/what-steps-do-you-take-to-troubleshoot-problems-with-php-curl) – hakre Dec 18 '12 at 10:38
  • 2
    You can **not** access SSL protected sites with cURL as default, you should be using a list of trusted certificates to do this, see this [article](http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/) on the subject. – adeneo Dec 18 '12 at 10:46

1 Answers1

1

That's actually a HTTPS link, i.e. it uses SSL, and you need to get cacert.pem, and set up cURL for SSL to make that work.

You can get the certificate here!

and you would set it up like so:

$curl = curl_init();
$browser = $_SERVER['HTTP_USER_AGENT'];
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: ";
curl_setopt($curl, CURLOPT_FAILONERROR,true);
curl_setopt($curl, CURLOPT_USERAGENT, $browser);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_AUTOREFERER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); //needed for SSL
curl_setopt($curl, CURLOPT_CAINFO, "/scripts/cacert.pem"); //path to file
curl_setopt($curl, CURLOPT_URL, $url_userInfo);
$content = curl_exec($curl);
echo curl_error($curl);  //display errors under development
curl_close($ch);
print_r( $content );

Using a proper user agent, and authenticating with SSL and a certificate, just like the browser would.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • 1
    @RanaMuhammadUsman - I added an `echo error` statement, try that and see what errors there are. Did you download the certificate and place it on the server with the correct path etc. – adeneo Dec 18 '12 at 10:41
  • Try increasing the `CONNECTTIMEOUT`, right now it's 2 seconds, which should be enough, as any more than 2 seconds for establishing a connection usually means the URL is not correct, but try increasing that number to 20 seconds and see if it connects or not. – adeneo Dec 18 '12 at 11:07
  • 1
    If your printing it to a browser, you'll probably have to use "view source" to see the tags etc. – adeneo Dec 18 '12 at 11:21