cURL PHP RESTful service always returning FALSE

13,605

$response is likely false because curl_exec() returns false (i.e., failure) into $result. Echo out curl_error($ch) (after the call to curl_exec) to see the cURL error, if that's the problem.

On a different note, I think your CURLOPT_POSTFIELDS is in an invalid format. You don't pass a JSON string to that.

This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.

-- PHP docs for curl_setopt()


Update

The quick way to avoid the SSL error is to add this option:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

You get an SSL verification error because cURL, unlike browsers, does not have a preloaded list of trusted certificate authorities (CAs), so no SSL certificates are trusted by default. The quick solution is to just accept certificates without verification by using the line above. The better solution is to manually add only the certificate(s) or CA(s) you want to accept. See this article on cURL and SSL for more information.

Share:
13,605
Admin
Author by

Admin

Updated on June 22, 2022

Comments

  • Admin
    Admin about 2 years

    I am having some difficulties POSTing a json object to an API that uses REST. I am new to using cURL, but I have searched all over to try to find an answer to my problem but have come up short. My cURL request is always returning false. I know it isn't even posting my json object because I would still get a response from the url. My code is below.

    <?php
    
    //API KEY = APIUsername
    //API_CODE = APIPassword
    $API_Key = "some API key";
    $API_Code = "some API code";
    $API_email = "[email protected]";
    $API_password = "ohhello";
    $url = "http://someURL.com/rest.svc/blah";
    
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    header('Content-type: application/json');
    $authData = "{\"APIUsername\":\"$API_Key\",\"APIPassword\":\"$API_Code\",\"EmailAddress\":\"$API_email\",\"Password\":\"$API_password\"}";
    curl_setopt($ch, CURLOPT_POSTFIELDS, $authData);
    
    //make the request
    $result = curl_exec($ch);
    $response = json_encode($result);
    echo $response;
    
    curl_close() 
    ?>
    

    The $response returns just "false"

    Any thoughts?

  • Admin
    Admin about 13 years
    On curl_error($ch) I get the message below. Is this a problem with the url that I am trying to post to? "SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed"
  • Wiseguy
    Wiseguy about 13 years
    I think that's pretty common. Try curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);.
  • Admin
    Admin about 13 years
    Worked perfect. Thanks for the help!
  • www.amitpatil.me
    www.amitpatil.me over 12 years
    I spent 4 days on one problem and then i found ur post "CURLOPT_SSL_VERIFYPEER" did the trick for me :) :) :)4