【问题标题】:Post Json through API using curl, no results使用 curl 通过 API 发布 Json,没有结果
【发布时间】:2015-06-01 13:49:39
【问题描述】:

我对此很陌生,所以希望它不是我错过的愚蠢的东西。我正在尝试为使用 API 的人创建订单。

我已经搜索了几个小时如何在 stackoverflow 上执行此操作,所有帖子都提供了很大的帮助。但现在我撞到了一堵砖墙。此代码在访问 PHP 页面时似乎没有抛出任何错误,也不会像预期的那样将商品从库存中取出。

我是否遗漏了一些非常简单的东西,还有没有办法检查它是否工作?

<?php

$today = date("D M j Y G:i:s T");
$data = array(
	"order" => [
	  "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',                                                                                                                               
	'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);
?>

【问题讨论】:

    标签: php api post curl


    【解决方案1】:

    没有以正确的方式设置Authorization 标头。应该这样做:

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

    出于调试目的,添加:

    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    

    最后,您应该为此服务创建一个新的访问令牌,因为您的访问令牌现在是公开的。

    【讨论】:

    • 谢谢!我已经更改了访问令牌,那是我非常愚蠢。我现在收到此错误 - {"status":"500","error":"Internal Server Error"},看起来很笼统,有什么想法吗?
    猜你喜欢
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2017-10-22
    • 1970-01-01
    • 2011-03-31
    • 2013-09-09
    相关资源
    最近更新 更多