SSL Certificate with PHP CURL

15,306

Solution 1

For my particular case i needed to add the keyfile, sslcert and cert password.

   //$xml = file_get_contents("thexmlfile.xml");
  $xml= $propertyXml->asXML();
  $ch = curl_init();

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '\pemfile.pem');

  curl_setopt($ch, CURLOPT_URL, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_SSH_PRIVATE_KEYFILE, getcwd() . '\myjks.jks');
  curl_setopt($ch, CURLOPT_SSLCERT, getcwd() . '\pemfile.pem');
  curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "thesslpassword");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($ch, CURLOPT_REFERER, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_VERBOSE , 1);

 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

  $ch_result = curl_exec($ch);
print curl_errno($ch);
print curl_error($ch);
  echo "Result = ".$ch_result;
  curl_close($ch);

Solution 2

It is failing as curl is unable to verify the certificate provided by the server.

There are two options to get this to work:

1 Allows curl to make insecure connections, that is curl does not verify the certificate.

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

2 Add the root CA (the CA signing the server certificate) in php.ini

curl.cainfo=/path/to/cacert.pem

You should use option 2 as thats the option that ensures that you are connecting to secure ftp server.

Share:
15,306

Related videos on Youtube

Gaz Smith
Author by

Gaz Smith

Updated on September 23, 2022

Comments

  • Gaz Smith
    Gaz Smith over 1 year

    I'm using curl to send an xml file over https to the rightmove API - they supplied me with all the certificates.

    I am getting the error :

    60SSL certificate problem: unable to get local issuer certificateResult =

    I have tried everything i have found on every other stackoverflow post similar and nothing is working, i have tried downloading the old cacert.pem and changed the files in php.ini - ive installed my personal information file andcreated a certificate on the browser and local machine and nothing is removing the error 60.

    This is my PHP :

    <?php
      $xml = file_get_contents("myxml.xml");
    
      $ch = curl_init();
    
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
      curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__).'\mypem.pem');
    
      curl_setopt($ch, CURLOPT_URL, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_POST, 1);
    
      curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
      curl_setopt($ch, CURLOPT_REFERER, 'https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails');
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt ($ch, CURLOPT_VERBOSE , 1);
    
      $ch_result = curl_exec($ch);
    print curl_errno($ch);
    print curl_error($ch);
      echo "Result = ".$ch_result;
      curl_close($ch);
    
    ?>
    

    this has had me banging my head for days, i would be very grateful for any assistance.

  • Gaz Smith
    Gaz Smith about 8 years
    I tired option 2 with cacert.pem (multiple ones i found on the internet from other posts) and it doesn't change anything.
  • Gaz Smith
    Gaz Smith about 8 years
    And ofcourse the api returns auth failed if i try to not verify the host
  • Gaz Smith
    Gaz Smith about 8 years
    when i change the curl.cainfo i get this : 60SSL certificate problem: unable to get local issuer certificateResult =
  • Tomasz Ferfecki
    Tomasz Ferfecki about 8 years
    @GazSmith using curl ssl check this link
  • Gaz Smith
    Gaz Smith about 8 years
    Thanks @walkingRed, although i still having an authentication issue from rightmoves api so the certificates must be wrong somewhere.
  • tousif
    tousif about 8 years
    Can you please paste the whole curl request please. I am struggling too with Curl Request for Rightmove for couple of days.
  • tousif
    tousif almost 6 years
    Thanks, This helped me till date