简体   繁体   中英

Set up a cURL to call a REST API

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM