1

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_HTTPAUTH, CURLAUTH_BASIC);
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);
curl_close($curl);

var_dump($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.

8
  • Using curl_error just after curl_exec it says "Warning: curl_error() expects parameter 1 to be resource, boolean given" Commented Jan 20, 2013 at 22:55
  • curl_error($curl_response); is used before curl_close(); Commented Jan 20, 2013 at 22:59
  • try curl_error($curl) and tell us what it says Commented Jan 20, 2013 at 23:36
  • nothing is returned to the view when I do that Commented Jan 20, 2013 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)' Commented Jan 20, 2013 at 23:40

1 Answer 1

4

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.