PUT to Restful API using PHP cURL

Hi there

I need to use cURL to change the status of an order on a Restful API using PHP. The instructions I received from the developers of the API is as follows:

curl -X PUT \
-d
“order[status]=S”
[site url]

In addition to this, they informed me that it is necessary to be signed in, with the credentials supplied.

So, my problems are these:

  1. what is the syntax for the PUT action in PHP cURL?
  2. how do I sign in to the site with PHP?

Any guidance will be appreciated.

Thanks.

Here is how you can PUT data with cURL in PHP: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl

As for logging in, you’d have to ask the developer of the API, as it differs per API what kind of action(s) is/are required to log in.

Hi

Thanks for the feedback. It turned out that the authentication was what was getting me stuck.

This is what eventually worked:

<?php

$order = “[API order no]“;
$data = http_build_query(array(“order[status]” => “S”));
$email = “[email address]”;
$password = “[password]";
$url = “[API URL]/$order”;

$ch=curl_init();

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($ch, CURLOPT_USERPWD, “$email:$password”);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “PUT”);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);
curl_close($ch);

?>

Regards

Mills

Weird that he would use PUT for that, but glad you got it working! :slight_smile: