1

I am having trouble getting either a cURL request or file_get_contents() for a https page that returns JSON. Please note that I have looked at multiple StackOverflow topics with similar problems, but none seem to address my specific issue.

BACKGROUND: I am using a MAC running OS X 10.10.3 (Yosemite). I just installed XAMPP 5.6.8 so that I can run a local web server in order to play around with php, etc.

GOAL: I would like to simply display the contents of a JSON object returned by a https GET request. For context, the page is https://api.discogs.com/releases/249504.

ACTIONS: I have read on other posts that for retrieving HTTPS pages I must make the following out-of-the-box changes in my php.ini file, which I have done: - uncomment/turn on allow_url_fopen=On - uncomment/turn on allow_url_include=on - uncomment extension=php_openssl.dll

RESULTS: Using file_get_contents()...

Code:

<?php
  $url = "https://api.discogs.com/releases/249504";
  $text = file_get_contents($url);
  var_dump($text);
?>

Result:

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /Applications/XAMPP/xamppfiles/htdocs/phpWorkspace/firstPhp/firstPhp.php on line 5

Warning: file_get_contents(): Failed to enable crypto in /Applications/XAMPP/xamppfiles/htdocs/phpWorkspace/firstPhp/firstPhp.php on line 5

Warning: file_get_contents(https://api.discogs.com/releases/249504): failed to open stream: operation failed in /Applications/XAMPP/xamppfiles/htdocs/phpWorkspace/firstPhp/firstPhp.php on line 5

bool(false)

Using cURL...

Code: https://api.discogs.com/releases/249504";

//  Initiate curl
  $ch = curl_init();

// Will return the response, if false it print the response
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Set the url
  curl_setopt($ch, CURLOPT_URL,$url);

// Execute
  $result=curl_exec($ch);

// Closing
  curl_close($ch);

  var_dump($result);
?>

Result: bool(false)

Any recommendations would be extremely helpful. I've played around with different cURL calls based on other SO posts, each have either returned NULL, an error related to certificates/authentication, or like above.

UPDATE!

I added CURLOPT_SSL_VERIFYPEER to my cURL code and set it to off in order to validate that I could at least communicate with the server - which helped me then identify that I needed to provide a user agent. However, when I removed the SSL_VERIFYPEER, I still had certificate issues.

As a last ditched effort I uninstalled XAMPP, and turned on the php/apache from within the Mac OS. Once configured, out-of-the-box both the the cURL and FILE_GET_CONTENTS() calls worked with the user agent using the same code as above [frustrating!].

My gut tells me that I had a bad configuration in XAMPP related to my certificates. Interestingly though I didn't see much online as this being an issue with other XAMPP users.

nickm
  • 11
  • 2
  • possible duplicate of [file\_get\_contents(): SSL operation failed with code 1. And more](http://stackoverflow.com/questions/26148701/file-get-contents-ssl-operation-failed-with-code-1-and-more) – Blue Jun 12 '15 at 20:36
  • false = curl failed, and `curl_error($ch)` will tell you why. but the SSL error is clear enough - your ssl lib wasn't able to verify the ssl cert of the remote site, therefore it aborted the connection. – Marc B Jun 12 '15 at 20:36

1 Answers1

0

Try adding this

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);

The certificate is not being successfully verified. This may not correct it depending on how severe the issue is.

You should not rely on this as a fix, however it should tell you if your system can even communicate with theirs. Reference the post Frankzers posted.

Thomas
  • 1,401
  • 8
  • 12
  • Can you please clarify why `curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);` is fairly bad security wise. – Blue Jun 12 '15 at 20:37
  • At this point he needs to see if it's even connecting to the server. * Updates post * – Thomas Jun 12 '15 at 20:37
  • @Thomas - Adding VERIFYPEER alone did not help, however adding it in conjunction with a user agent curl_setopt(..., CURLOPT_USERAGENT, ...) did work! Now, I've read in a host of places that having CURLOPT_SSL_VERIFYPEER set to off is bad. Any further thoughts as to what might be going on? I'll note that using a USERAGENT alone did not work without the CURLOPT_SSL_VERIFYPEER set to false... – nickm Jun 12 '15 at 23:12