Ali Zia Ali Zia - 15 days ago 4
PHP Question

Convert POSTMAN Data to array / object to send it to server

I have a postman API. This is the screenshot.

enter image description here

I want to send this data to my url using PHP Curl. I tried everything but it says that merchant_id is missing. Please can anyone guide me how to post these parameters to CURL and how to get appropriate response? Thanks in advance.

UPDATE

This is my PHP Code.

$form_data = json_decode($_POST['form_data']);
$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 = json_encode($data);
// $data = '{
// "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"
// }';

$url = 'https://ship2you.com/ship2you/';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));


// if(curl_exec($ch) === false)
// {
// echo 'Curl error: ' . curl_error($ch);
// } else {
$result = curl_exec($ch);
//}

curl_close($ch);

$json_result = json_decode($result, true);
echo '<pre>';print_r($json_result);echo '</pre>';

Answer

Try my code .

   $form_data = json_decode($_POST['form_data']);
    $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 = json_encode($data);
    // $data = '{
    // "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"
    // }';

    $url = 'https://ship2you.com/ship2you/';

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

    // if(curl_exec($ch) === false)
    // {
    //     echo 'Curl error: ' . curl_error($ch);
    // } else {
        $result = curl_exec($ch);
    //}

    curl_close($ch);

    $json_result = json_decode($result, true);
    echo '<pre>';print_r($json_result);echo '</pre>';
Comments