2016-11-17 77 views
1

我有一個郵遞員API。這是截圖。將POSTMAN數據轉換爲數組/對象以將其發送到服務器

enter image description here

我想這個數據發送到使用PHP捲曲我的網址。我嘗試了一切,但它說,merchant_id丟失。請任何人都可以指導我如何將這些參數發佈到CURL以及如何獲得適當的響應?提前致謝。

UPDATE

這是我的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

請發佈您的PHP代碼 –

+0

嘗試刪除斜槓!那些斜槓是逃避報價 – Ima

+0

@KanishkaPanamaldeniya我上傳了我的代碼。 –

回答

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>'; 
+1

工作就像一個魅力。謝謝分配。 –

+0

@AliZia歡迎您:) –

相關問題