homepage Welcome to WebmasterWorld Guest from 54.145.183.190
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PUT to Restful API using PHP cURL
Inspector Mills



 
Msg#: 4663728 posted 8:31 am on Apr 17, 2014 (gmt 0)

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.

 

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4663728 posted 1:08 pm on Apr 18, 2014 (gmt 0)

The developers of the API are showing you the cURL command line syntax so you can either execute a system command or, and more likely the case, you are going to need to convert that command line syntax to the PHP cURL API syntax and execute your command.

Here are some helpful resources to get you started:

The PHP cURL documentation:
[php.net...]

... and specifically the cURL set options:
[php.net...]

The cURL site, specifically the set opts man page:
[curl.haxx.se...]

And here is a PHP tutorial on how to use PUT (many other tutorials on this page that you will want to read and absorb too!):
[curl.haxx.se...]
[curl.haxx.se...]

Inspector Mills



 
Msg#: 4663728 posted 8:21 pm on May 6, 2014 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved