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

6
  • Check whether Curl gives any error using curl_error. You can refer this answer to find error. Commented Nov 16, 2016 at 12:42
  • Have you tried the code generated by postman? Commented Nov 16, 2016 at 12:44
  • Curl error: SSL certificate problem: self signed certificate in certificate chain @BhavikShah Commented Nov 16, 2016 at 12:46
  • You should check this answer. It has something to do with the certificate. Commented Nov 16, 2016 at 12:51
  • @BhavikShah I already checked it at it worked. Thanks. Commented Nov 16, 2016 at 12:55

2 Answers 2

34

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

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

1 Comment

in some different postman version: code icon '</>' is used
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.

4 Comments

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.
@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.
@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.
@AliZia: cURL Worked with static text??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.