jtubre
jtubre

Reputation: 699

Send BOTH Parameters and Body Content with PHP POST cURL

I need to send both key value pair parameters and HTTP body content (in this case, an XML string) using PHP. cURL is my go to method for similar tasks, but I can't figure out how to send both with a single cURL Post request.

I can use CURLOPT_POSTFIELDS to send an array of key value pairs:

curl_setopt($c, CURLOPT_POSTFIELDS, array('foo' => 'bar', 'fruit' = 'orange));

OR I can use CURLOPT_POSTFIELDS to send an XML string.

curl_setopt($c, CURLOPT_POSTFIELDS,$xml);

The API docs require both request parameters and a body in the single post request. This API also throttles requests so I'm limited in my debug attempts per hour. Please help.

Edit: API Docs (see examples at bottom): http://docs.developer.amazonservices.com/en_US/feeds/Feeds_SubmitFeed.html

Upvotes: 1

Views: 3074

Answers (1)

hanshenrik
hanshenrik

Reputation: 21513

you have 3 places you can put data, the URL itself (which is technically not part of the headers, this is where php gets the $_GET data from), the headers (php put this in the $_SERVER variable), and the post body.

you must choose which of these 3 places you want to put your data. most likely, what you want, is to put array('foo' => 'bar', 'fruit' = 'orange') in the GET url, to do that, do: $url='http://example.com?'.http_build_query(array('foo' => 'bar', 'fruit' = 'orange'));

  • but if you really want to send both the POST parameters, and the XML in the http request body, which is probably not what you want, but if you're really sure, it would be: curl_setopt($c, CURLOPT_POSTFIELDS,http_build_query(array('foo' => 'bar', 'fruit' = 'orange').$xml); - aka, concatenate the strings. but this will result in an invalid XML file, and is probably not what you want.

  • if you want to put the data in the headers, use CURLOPT_HTTPHEADER, but make sure its formatted as keyname then : then a space then the value, but there's lots of bytes that are illegal for headers, off the top of my head, \r\n<>\x00 - but there's more too.

Upvotes: 2

Related Questions