Reputation: 8083
I've been trying to setup a cURL call to a REST API for days (see my previous topics) and still it won't work...
Though, what I need is fairly simple:
I need make a call with the POST method to an url. This URL requires authentication (which, according to the documentation, I should pass by HTTP headers using the GET function). And on top of that, I need to set a body (<searchCriteriaSorting></searchCriteriaSortin
) and I should use the Content-Type: application/xml
and the Charset=UTF-8
.
Nothing more, nothing less. According to the author of the API, it should be very simple. But me, nor my colleagues manage to build a solid connection to this API.
Upvotes: 0
Views: 3978
Reputation: 1497
To use authentication you can use the cURL option CURLOPT_USERPWD. To define that you'd like to use POST use cURL option CURLOPT_POST. To set the required headers you can use use cURL option CURLOPT_HTTPHEADER. Finally you have to set the body of the HTTP POST request be using cURL option CURLOPT_POSTFIELDS, whereby $xml is your XML content.
/* init cURL session */
$ch = curl_init();
...
/* set password */
curl_setopt($ch, CURLOPT_USERPWD, "loginname:passwort");
/* use method POST */
curl_setopt($ch, CURLOPT_POST, true);
/* set header 'Content-Type' */
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=UTF-8'));
/* set HTTP body */
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
...
/* exec cURL request */
curl_exec($ch);
curl_close($ch);
Upvotes: 6