note 96056 added to function.curl-setopt

From: Date: Fri, 05 Feb 2010 18:47:22 +0000
Subject: note 96056 added to function.curl-setopt
Groups: php.notes 
Request: Send a blank email to php-notes+get-165693@lists.php.net to get a copy of this message
PUT requests are very simple, just make sure to specify a content-length header and set post fields as a string. Example: function doPut($url, $fields) { $fields = (is_array($fields)) ? http_build_query($fields) : $fields; if($ch = curl_init($url)) { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($fields))); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return (int) $status; } else { return false; } } if(doPut('http://foo.com/api/a/b/c', array('foo' => 'bar')) == 200) // do something else // do something else. You can grab the request data on the other side with: if($_SERVER['REQUEST_METHOD'] == 'PUT') { parse_str(file_get_contents('php://input'), $requestData); // Array ( [foo] => bar ) print_r($requestData); // Do something with data... } DELETE can be done in exactly the same way. ---- Server IP: 69.147.83.197 Probable Submitter: 91.107.8.2 ---- Manual Page -- http://www.php.net/manual/en/function.curl-setopt.php Edit -- https://master.php.net/note/edit/96056 Del: integrated -- https://master.php.net/note/delete/96056/integrated Del: useless -- https://master.php.net/note/delete/96056/useless Del: bad code -- https://master.php.net/note/delete/96056/bad+code Del: spam -- https://master.php.net/note/delete/96056/spam Del: non-english -- https://master.php.net/note/delete/96056/non-english Del: in docs -- https://master.php.net/note/delete/96056/in+docs Del: other reasons-- https://master.php.net/note/delete/96056 Reject -- https://master.php.net/note/reject/96056 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165693) next »