Take the 2-minute tour ×

Need to consume some data from a web service that requires a username/password for access.

The following returns NULL

$service_url = 'https://example.com/2365139.json';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);


When I hit https://example.com/2365139.json in a browser, it prompts for un/pw and when I enter them it displays the JSON, so the data is there but something I have written above isn't working.

share|improve this question
Using curl_error just after curl_exec it says "Warning: curl_error() expects parameter 1 to be resource, boolean given" –  Adrian Jan 20 '13 at 22:55
curl_error($curl_response); is used before curl_close(); –  Adrian Jan 20 '13 at 22:59
try curl_error($curl) and tell us what it says –  Ivan Jan 20 '13 at 23:36
nothing is returned to the view when I do that –  Adrian Jan 20 '13 at 23:37
When I var_dump($curl_response) it returns 'bool(false)' and when I var_dump($curl) it returns 'resource(194) of type (curl)' –  Adrian Jan 20 '13 at 23:40
show 3 more comments

1 Answer

up vote 2 down vote accepted

Original code works fine, but because the resource is https it requires the following option;

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

The caveat to using this is "This basically causes cURL to blindly accept any server certificate, without doing any verification as to which CA signed it, and whether or not that CA is trusted. If you’re at all concerned about the data you’re passing to or receiving from the server, you’ll want to enable this peer verification properly." - Taken from http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

share|improve this answer
add comment

Your Answer


