I'm a little confused on how to set up a cURL request to an API I'm working with. To be specific, it's for a fulfillment center called ShipStation (http://api.shipstation.com). I've done many cURL requests in the past but now I'm trying to figure out how to set up a 'MERGE' cURL request as opposed to a 'GET' cURL request, etc. You can see on here the 'GET' header to pull info from the API:
http://api.shipstation.com/Order-Resource.ashx#Reading_an_Order_5
And then to update/merge data:
http://api.shipstation.com/Order-Resource.ashx#Updating_an_Order_6
Every time I try to send a request though, I get curl_setopt(): supplied argument is not a valid cURL handle resource
errors on several lines. I tried initially by copying the data and trying to send it as a header:
$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);
I do update the username and password to my credentials since you need that to log into this API. I basically copied the header as it was and it doesn't work. I also updated 'CURLOPT_CUSTOMREQUEST' to 'CURLOPT_HTTPHEADER' but both gave errors.
I'm not understanding where I'm going wrong and I also don't know how (if it's possible) to return more detailed error messages so I can get to the bottom of the problem with the code since I just get the supplied argument
error.
Thanks for your help!
Perhaps I'm approaching this wrong? How would I send a 'MERGE' request as evidenced in the documentation in the links above. I don't know how to take that info that they've given (the header info) and translate it into a request to the API.
Try in this way:
$end = "
";
$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);
Please try this:
$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);
}