Ali Zia
Ali Zia

Reputation: 3875

How can I convert data from POSTMAN into PHP Curl Request?

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

Upvotes: 11

Views: 25818

Answers (2)

Juned Ansari
Juned Ansari

Reputation: 5275

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

Upvotes: 33

Bhavik Shah
Bhavik Shah

Reputation: 2291

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.

Upvotes: 5

Related Questions