将POSTMAN数据转换为数组/对象以将其发送到服务器

时间:2016-11-17 08:11:36

标签: php curl

我有一个邮差API。这是截图。

enter image description here

我想使用PHP Curl将此数据发送到我的网址。我尝试了一切,但它说缺少了merchant_id。请任何人都可以指导我如何将这些参数发布到CURL以及如何获得适当的响应?提前谢谢。

更新

这是我的PHP代码。

$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>';

1 个答案:

答案 0 :(得分:1)

试试我的代码。

   $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>';