Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

curl

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

like image 659
Ali Zia Avatar asked Nov 16 '16 12:11

Ali Zia


People also ask

Is Postman same as cURL?

Postman is a platform for testing APIs. cURL is a command-line utility that allows you to transmit data over URLs. To do the same operations with REST APIs, we may use Postman as a GUI (graphical user interface) and cURL as a CLI (command-line interface).


1 Answers

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

like image 90
Juned Ansari Avatar answered Oct 16 '22 16:10

Juned Ansari