设置GET cURL请求(PHP)

时间:2014-06-04 17:57:04

标签: php curl

我对如何设置对我正在使用的API的cURL请求感到有些困惑。具体而言,它适用于称为ShipStation(http://api.shipstation.com)的履行中心。我过去做过很多cURL请求,但现在我想弄清楚如何设置一个' MERGE' cURL请求而不是' GET' cURL请求等您可以在此处看到' GET'标头从API中提取信息:

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 argument is not a valid cURL handle resource错误。我最初尝试复制数据并尝试将其作为标题发送:

$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'但两者都有错误。

我不明白我哪里出错了,而且我也不知道(如果可能的话)如何返回更详细的错误消息以便我可以到底因为我刚收到supplied argument错误而导致代码问题。

感谢您的帮助!

修改

也许我接近这个错误?我如何发送一个' MERGE'请求在上面链接的文档中证明。我不知道如何获取他们已经提供的信息(标题信息)并将其转换为对API的请求。

2 个答案:

答案 0 :(得分:0)

尝试这种方式:

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

答案 1 :(得分:0)

请试试这个:

$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);
}