2015-06-01 78 views
0

我對此相當新,所以希望它不是愚蠢的我錯過了。我正在嘗試爲使用API​​的人創建訂單。發佈Json通過API使用捲曲,沒有結果

我已經搜索了幾個小時如何做到這一點在stackoverflow和所有的帖子都有很大的幫助。但是現在我撞到了一堵磚牆。這段代碼在訪問PHP頁面時似乎不會引發任何錯誤,也不會像其意圖那樣將該項目從庫存中取出。

我錯過了一些非常簡單的東西,還有沒有辦法檢查它的工作?

<?php 
 

 
$today = date("D M j Y G:i:s T"); 
 
$data = array(
 
\t "order" => [ 
 
\t "order_number" => "", 
 
      "company_id" => 690094, 
 
      "billing_address_id" => 1018327, 
 
      "shipping_address_id" => 1018327, 
 
      "stock_location_id" => 16377, 
 
      "ship_at" => $today, 
 
      "issued_at" => $today, 
 
      "tax_type" => "exclusive", 
 
      "payment_status" => "unpaid", 
 
      "fulfillment_status" => "shipped", 
 
      "email" => null, 
 
      "reference_number" => 7784, 
 
      "status" => "fulfilled", 
 
      "order_line_items" => [ 
 
       "quantity" => 1, "discount" => null, "price" => 1, 
 
       "tax_rate_override" => null, "freeform" => false, 
 
       "variant_id" => 6983077 
 
      ], 
 
     ], 
 
); 
 

 
$url ="https://api.tradegecko.com/orders/"; 
 
$str_data = json_encode($data); 
 

 
function sendPostData($url, $str_data){ 
 
    $ch = curl_init($url); 
 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
 
    'Content-Type: application/json', 
 
    'Accept: application/json',                                
 
\t 'http' => array(
 
     'header' => "Authorization: Bearer 872ed5e72dfc7e4e0adaa7c663cab7d81415078fdbe4f486d085b718c93b3eb3") 
 
    )                  
 
); 
 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$str_data); 
 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
 
    $result = curl_exec($ch); 
 
    curl_close($ch); // Seems like good practice 
 
    return $result; 
 
} 
 

 
echo sendPostData($url, $str_data); 
 
?>

回答

0

設置Authorization頭沒有以正確的方式來完成。它應該做的事,如:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
    'Content-Type: application/json', 
    'Accept: application/json',                                
    'Authorization: Bearer 872ed5e72dfc7e4e0adaa7c663cab7d81415078fdbe4f486d085b718c93b3eb3') 
); 

爲了調試的目的,是有益的補充:

curl_setopt($ch, CURLOPT_VERBOSE, 1); 

最後,你應該爲此服務創建一個新的訪問令牌,因爲你現在是公衆。

+0

感謝您的支持!我改變了訪問令牌,那是我非常愚蠢的。 我現在得到這個錯誤 - {「狀態」:「500」,「錯誤」:「內部服務器錯誤」},似乎非常通用,任何想法? –