note 109634 added to function.curl-setopt

From: Date: Fri, 03 Aug 2012 08:57:56 +0000
Subject: note 109634 added to function.curl-setopt
Groups: php.notes 
Request: Send a blank email to php-notes+get-189907@lists.php.net to get a copy of this message
If you are trying to update something on your server and you need to handle this update operation by PUT; <?php curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_PUT, 1); ?> are "useless" without; <?php curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT')); ?> Example; Updating a book data in database identified by "id 1"; --cURL Part-- <?php $data = http_build_query($_POST); // or $data = http_build_query(array( 'name' => 'PHP in Action', 'price' => 10.9 )); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://api.localhost/rest/books/1"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // no need anymore // or // curl_setopt($ch, CURLOPT_PUT, 1); // no need anymore curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT')); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $ce = curl_exec($ch); curl_close($ch); print_r($ce); ?> --API class-- <?php public function putAction() { echo "putAction() -> id: ". $this->_getParam('id') ."\n"; print_r($_POST); // do stuff with post data ... ?> --Output-- putAction() -> id: 15 Array ( [name] => PHP in Action [price] => 10.9 ) ---Keywords-- rest, restfull api, restfull put, curl put, curl customrequest put ---- Server IP: 160.75.100.7 Probable Submitter: 85.101.222.218 ---- Manual Page -- http://www.php.net/manual/en/function.curl-setopt.php Edit -- https://master.php.net/note/edit/109634 Del: integrated -- https://master.php.net/note/delete/109634/integrated Del: useless -- https://master.php.net/note/delete/109634/useless Del: bad code -- https://master.php.net/note/delete/109634/bad+code Del: spam -- https://master.php.net/note/delete/109634/spam Del: non-english -- https://master.php.net/note/delete/109634/non-english Del: in docs -- https://master.php.net/note/delete/109634/in+docs Del: other reasons-- https://master.php.net/note/delete/109634 Reject -- https://master.php.net/note/reject/109634 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#189907) next »