1

I am trying to reverse engineer an api due to a lack of documentation from our software provider and I need to recreate a curl command in php. The command in question is:

curl -v -X POST -H Accept-Encoding:"gzip" -H deviceId:"someID" -H appBuildDate:"2014-12-11 15:56:52 +0000" -H systemName:"iPhone OS" -H appBuildNumber:"44" -H platform:"iPhone5,2" -H Content-Type:"application/xml" -H model:"iPhone" -H name:"iphone" -H systemVersion:"8.1.2" -H Authorization:"Token someToken" -H appVersion:"6.8" https://soemsub.somedomain.com/ws/orders/search -d "<orderSearch><consigneeSearch /><customerSearch /><shipperSearch scheduledArrivalEarly=\"12/17/2014\" /></orderSearch>"

The only problem I am having is that I'm not sure what options to set for the -d command.

I have set CURLOPT_POST to true and CURLOPT_POSTFIELDS using:

$query = "<orderSearch><consigneeSearch /><customerSearch /><shipperSearch scheduledArrivalEarly=\"$date\" /></orderSearch>";
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $query);

I have also tried:

curl_setopt($this->curl, CURLOPT_POSTFIELDS, array($query));

And I'm not getting valid responses. Is there a trick to getting this to post correctly? The command I outlined at the top is written as-is in the log file(aside from deviceId and Authorization), but I'm not sure if I can specify a post without a post "name" like I would do in an HTML form. From what I can tell I need to have key=>value pairs.

Am I missing something? Thanks in advance!

RattleyCooper
  • 4,997
  • 5
  • 27
  • 43

1 Answers1

1

Set your headers without any quotes around the keys value:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Authorization:Token someToken',
  'deviceId:someID',
  // ... all the other headers
));

Instead of:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Authorization:"Token someToken"',
  'deviceId:"someID"',
  // ... all the other headers
));

If you use POSTFIELDS with a string body, you need to urlencode the it:

$query = "<orderSearch><consigneeSearch /><customerSearch /><shipperSearch scheduledArrivalEarly=\"$date\" /></orderSearch>";
curl_setopt($this->curl, CURLOPT_POSTFIELDS, urlencode($query));

See this other question: Should I URL-encode POST data?

The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data

Community
  • 1
  • 1
Ray
  • 40,256
  • 21
  • 101
  • 138
  • I'm testing this now. I think I need to specify the `Content-Type` as `"application/xml"`. I took a look at that question you posted and I'm pretty sure I need to specify that content type. I looked at the headers in my log files and they include it for this type of request so I think that is it! – RattleyCooper Dec 17 '14 at 19:17
  • Yeah from the examples that I see passing the string in without urlencode should be fine you probably just need to set the header. – Marc Seiler Dec 17 '14 at 19:19
  • @DuckPuncher Sorry I thought you were also setting your headers. – Ray Dec 17 '14 at 19:20
  • @DigitalFiz I assume the headers were already being set, I've updated my answer to add them – Ray Dec 17 '14 at 19:22
  • 1
    @Ray add the Content-Type header also – Marc Seiler Dec 17 '14 at 19:22
  • @Ray, I am, just not that one. Whoops! Even when I set the header it doesn't seem to be working. I'll have to toy with it a bit.. – RattleyCooper Dec 17 '14 at 19:22
  • @DigitalFiz I don't see content type being set in the original working curl request – Ray Dec 17 '14 at 19:23
  • @Ray nope ignore me :P – Marc Seiler Dec 17 '14 at 19:26
  • @Ray, it's in the original command... `curl -v -X POST -H Accept-Encoding:"gzip" -H deviceId:"someID" -H appBuildDate:"2014-12-11 15:56:52 +0000" -H systemName:"iPhone OS" -H appBuildNumber:"44" -H platform:"iPhone5,2" -H Content-Type:"application/xml" -H model:"iPhone" -H name:"iphone" -H systemVersion:"8.1.2" -H Authorization:"Token someToken" -H appVersion:"6.8" https://soemsub.somedomain.com/ws/orders/search -d ""` I had removed what I assumed was unimportant :/ – RattleyCooper Dec 17 '14 at 19:26
  • @DuckPuncher ah. You should add that to the OP. All those headers need to be added to the header array – Ray Dec 17 '14 at 19:27
  • 1
    @DuckPuncher Not positive, but you might be able to ditch the double quotes on each header string: ``'Authorization:Token someToken'`` I think command line curl needs them to demark the string, while in php it's done by the array element itself – Ray Dec 17 '14 at 19:33
  • @Ray, :D You are my best friend right now Ray. That was it! – RattleyCooper Dec 17 '14 at 19:40
  • @DuckPuncher Rock on. – Ray Dec 17 '14 at 20:46