设置GET-cURL请求(PHP)
我对如何向正在使用的API设置cURL请求有点困惑。具体来说,这是一个叫做ShipStation的履行中心。我过去做过很多cURL请求,但现在我正试图找出如何设置“MERGE”cURL请求,而不是“GET”cURL请求,等等。您可以在这里看到“GET”头以从API获取信息:设置GET-cURL请求(PHP),php,curl,Php,Curl,我对如何向正在使用的API设置cURL请求有点困惑。具体来说,这是一个叫做ShipStation的履行中心。我过去做过很多cURL请求,但现在我正试图找出如何设置“MERGE”cURL请求,而不是“GET”cURL请求,等等。您可以在这里看到“GET”头以从API获取信息: http://api.shipstation.com/Order-Resource.ashx#Reading_an_Order_5 然后更新/合并数据: http://api.shipstation.com/Order-R
http://api.shipstation.com/Order-Resource.ashx#Reading_an_Order_5
然后更新/合并数据:
http://api.shipstation.com/Order-Resource.ashx#Updating_an_Order_6
不过,每次尝试发送请求时,我都会发现curl_setopt:supplied参数不是有效的curl句柄,在几行上都有资源错误。我最初尝试复制数据并尝试将其作为标头发送:
$header .= "GET https://data.shipstation.com/1.3/Orders(128714) HTTP/1.1";
$header .= "User-Agent: Microsoft ADO.NET Data Services";
$header .= "Accept-Charset: UTF-8";
$header .= "DataServiceVersion: 1.0;NetFx";
$header .= "MaxDataServiceVersion: 2.0;NetFx";
$header .= "Accept: application/atom+xml,application/xml";
$header .= "Host: data.shipstation.com";
//Send request
$curlConn = curl_init();
curl_setopt($curlConn,CURLOPT_USERPWD,'myusername:mypassword');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
curl_setopt($curlConn,CURLOPT_RETURNTRANSFER,1);
$ret = curl_exec($curlConn);
curl_close($curlConn);
我会将用户名和密码更新到我的凭据中,因为您需要它才能登录到此API。我基本上是照着原样复制标题,但它不起作用。我还将“CURLOPT_CUSTOMREQUEST”更新为“CURLOPT_HTTPHEADER”,但两者都给出了错误
我不明白我哪里出错了,我也不知道如何才能返回更详细的错误消息,以便我能够找到代码问题的根源,因为我只得到了提供的参数错误
谢谢你的帮助
编辑
也许我的做法不对?我如何发送“合并”请求,如上面链接中的文档所示。我不知道如何获取他们提供的标题信息,并将其转换为对API的请求。请尝试以下方法:
$end = "\r\n";
$header .= "GET https://data.shipstation.com/1.3/Orders(128714) HTTP/1.1" . $end;
$header .= "User-Agent: Microsoft ADO.NET Data Services" . $end;
$header .= "Accept-Charset: UTF-8" . $end;
$header .= "DataServiceVersion: 1.0;NetFx" . $end;
$header .= "MaxDataServiceVersion: 2.0;NetFx" . $end;
$header .= "Accept: application/atom+xml,application/xml" . $end;
$header .= "Host: data.shipstation.com" . $end;
//Send request
$curlConn = curl_init();
curl_setopt($curlConn,CURLOPT_USERPWD,'myusername:mypassword');
curl_setopt($curlConn, CURLOPT_CUSTOMREQUEST, $header);
curl_setopt($curlConn,CURLOPT_RETURNTRANSFER,1);
$ret = curl_exec($curlConn);
curl_close($curlConn);
请试试这个:
$username = "YOUR API KEY";
$password = "YOUR API SECRET";
$endpoint = "https://ssapi.shipstation.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPHEADER, array ("
Content-Type: application/json"
));
create_order($ch, $endpoint);
function create_order($ch, $endpoint) {
curl_setopt($ch, CURLOPT_URL, $endpoint . "/orders/createorder");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
\"orderNumber\": \"ABC124\",
\"orderKey\": \"0f6aec18-3e89-4771-83aa-f392d84f4c74\",
\"orderDate\": \"2015-01-31T17:46:27.0000000\",
\"paymentDate\": \"2015-01-31T17:46:27.0000000\",
\"orderStatus\": \"awaiting_shipment\",
\"customerUsername\": \"headhoncho@whitehouse.gov\",
\"customerEmail\": \"headhoncho@whitehouse.gov\",
\"billTo\": {
\"name\": \"The President\",
\"company\": \"US Govt\",
\"street1\": \"1600 Pennsylvania Ave\",
\"street2\": \"Oval Office\",
\"street3\": null,
\"city\": \"Washington\",
\"state\": \"DC\",
\"postalCode\": \"20500\",
\"country\": \"US\",
\"phone\": null,
\"residential\": true
},
\"shipTo\": {
\"name\": \"The President\",
\"company\": \"US Govt\",
\"street1\": \"1600 Pennsylvania Ave\",
\"street2\": \"Oval Office\",
\"street3\": null,
\"city\": \"Washington\",
\"state\": \"DC\",
\"postalCode\": \"20500\",
\"country\": \"US\",
\"phone\": null,
\"residential\": true
},
\"items\": [
{
\"lineItemKey\": null,
\"sku\": \"ABC123\",
\"name\": \"Test item #1\",
\"imageUrl\": null,
\"weight\": {
\"value\": 24,
\"units\": \"ounces\"
},
\"quantity\": 2,
\"unitPrice\": 99.99,
\"warehouseLocation\": \"Aisle 1, Bin 7\",
\"options\": []
},
{
\"lineItemKey\": null,
\"sku\": \"DEF456\",
\"name\": \"Test item #2\",
\"imageUrl\": null,
\"weight\": {
\"value\": 0.01,
\"units\": \"ounces\"
},
\"quantity\": 3,
\"unitPrice\": 1.25,
\"warehouseLocation\": \"Aisle 7, Bin 34\",
\"options\": []
}
],
\"amountPaid\": 218.73,
\"taxAmount\": 5,
\"shippingAmount\": 10,
\"customerNotes\": null,
\"internalNotes\": \"This order was created via the ShipStation API\",
\"gift\": false,
\"giftMessage\": null,
\"requestedShippingService\": \"Priority Mail\",
\"paymentMethod\": null,
\"carrierCode\": \"fedex\",
\"serviceCode\": \"fedex_2day\",
\"packageCode\": \"package\",
\"confirmation\": \"delivery\",
\"shipDate\": \"2014-04-08\",
\"weight\": {
\"value\": 0,
\"units\": \"ounces\"
},
\"dimensions\": {
\"units\": \"inches\",
\"length\": 7,
\"width\": 5,
\"height\": 6
},
\"insuranceOptions\": {
\"provider\": null,
\"insureShipment\": false,
\"insuredValue\": 0
},
\"internationalOptions\": {
\"contents\": null,
\"customsItems\": null
},
\"advancedOptions\": {
\"warehouseId\": 34369,
\"nonMachinable\": false,
\"saturdayDelivery\": false,
\"containsAlcohol\": false,
\"storeId\": 42756,
\"customField1\": \"Some custom data\",
\"customField2\": null,
\"customField3\": null,
\"source\": null
}
}");
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
}
这不再抛出错误,但我也不会得到API的响应。我会再看一看,然后再打给你。这可能已经达到了目的。所以当我使用它并执行var_dump$ret时,我会收到一个boolfalse响应。这一定意味着它没有正确连接,即使我没有从API获得错误响应?这意味着连接失败