11

I have an API in postman. I want to create a CURL Request and get proper response with it. This is my POSTMAN API.

enter image description here

I am successfully getting this response with it.

"{\"Request\":{\"state\":\"Manama\",\"address\":\"406 Falcon Tower\",\"address2\":\"Diplomatic Area\",\"city\":\"Manama\",\"country\":\"BH\",\"fullname\":\"Dawar Khan\",\"postal\":\"317\"},\"Response\":{\"status\":\"Success\",\"code\":100,\"message\":\"Address is verified\"}}"

Now I want to use this API Call inside my PHP Code. I used this code.

    $data = array(
        'Request' => 'ValidateAddress',
        'address' => test_input($form_data->address),
        'secondAddress' => test_input($form_data->secondAddress),
        'city' => test_input($form_data->city),
        'country' => test_input($form_data->country),
        'name' => test_input($form_data->name),
        'zipCode' => test_input($form_data->zipCode),
        'merchant_id' => 'shipm8',
        'hash' => '09335f393d4155d9334ed61385712999'
    );
    $data_string = json_encode($data);

    $url = 'myurl.com/';

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
    );
    $result = curl_exec($ch);
    curl_close($ch);
    $json_result = json_decode($result, true);
    echo '<pre>';print_r($json_result);echo '</pre>';

But I can't see my $json_result. It just echoes <pre></pre> in the view. Can anyone guide me? Thanks in advance. I want to get my Response.

UPDATE

I used curl_error and it gives me the following error.

Curl error: SSL certificate problem: self signed certificate in certificate chain

Ali Zia
  • 3,825
  • 5
  • 29
  • 77
  • Check whether Curl gives any error using [**`curl_error`**](http://php.net/manual/en/function.curl-error.php). You can refer [**`this answer`**](http://stackoverflow.com/questions/3987006/how-to-catch-curl-errors-in-php#answer-3987037) to find error. – Bhavik Shah Nov 16 '16 at 12:42
  • Have you tried the code generated by postman? – barudo Nov 16 '16 at 12:44
  • Curl error: SSL certificate problem: self signed certificate in certificate chain @BhavikShah – Ali Zia Nov 16 '16 at 12:46
  • You should check [**`this answer`**](http://stackoverflow.com/questions/21187946/60-ssl-certificate-self-signed-certificate-in-certificate-chainbool#answer-23585500). It has something to do with the certificate. – Bhavik Shah Nov 16 '16 at 12:51
  • @BhavikShah I already checked it at it worked. Thanks. – Ali Zia Nov 16 '16 at 12:55
  • @AliZia: Great. I have drafted an answer as well for others who might face same issue. – Bhavik Shah Nov 16 '16 at 13:08

2 Answers2

32

It is Very Simple Just Click on Code you will get the code in php.

you will get the code in many language like php,java,ruby,javascript,nodejs,shell,swift,pythom,C# etc. enter image description here

enter image description here

Juned Ansari
  • 5,035
  • 7
  • 56
  • 89
5

Answer updated as per updated question.

There are two ways to solve this issue

Lengthy, time-consuming yet clean

  1. Visit URL in web browser.
  2. Open Security details.
  3. Export certificate.
  4. Change cURL options accordingly.

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/BuiltinObjectToken-EquifaxSecureCA.crt");
    

Quick but dirty

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

We are configuring cURL to accept any server(peer) certificate. This isn’t optimal from a security point of view.

Excerpt from very detailed and precise article with screenshots for better understanding. Kindly refer the same before actually implementing it in production site.

Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
  • Can you tell me 1 last thing? What do I have to send in $data_string so that my response is successfully sent? I am not getting any response yet and errors that my parameters are missing. – Ali Zia Nov 16 '16 at 13:09
  • @AliZia: I don't think, anything is missing in `$data_string`. But, you should verify that you have passed all required parameters. I mean, there is nothing wrong technically. May be, you are missing something some required parameters for successful request and response. – Bhavik Shah Nov 16 '16 at 13:12
  • @AliZia: Or you should try to test with static data in source. Just check, whether everything works fine with static data. If yes, then there is something wrong with the data or used functions. – Bhavik Shah Nov 16 '16 at 13:14
  • @AliZia: cURL Worked with static text?? – Bhavik Shah Nov 16 '16 at 13:24