test param:1104

From: Date: Wed, 25 Jan 2012 20:58:58 +0000
Subject: test param:1104
Groups: php.test 
Request: Send a blank email to php-test+get-920@lists.php.net to get a copy of this message
Simple REST client/server example with curl. client.php: <code> <?php $url = "http://localhost/server.php"; // GET method // init curl $ch = curl_init(); // Set options for GET curl_setopt($ch, CURLOPT_URL, $url."/42"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPGET, true); // Execute request $response = curl_exec($ch); echo "---- GET ----\nStatus-Code: ". curl_getinfo($ch, CURLINFO_HTTP_CODE)."\n"; echo $response."\n----\n"; curl_close($ch); // POST method // init curl $ch = curl_init(); // Set options for POST curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); // Set POST-Body $data = "Data 0815"; curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Execute request $response = curl_exec($ch); echo "---- POST ----\nStatus-Code: ". curl_getinfo($ch, CURLINFO_HTTP_CODE)."\n"; echo $response."\n----\n"; curl_close($ch); // PUT method // init curl $ch = curl_init(); // Set options for PUT curl_setopt($ch, CURLOPT_URL, $url."/42"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_PUT, true); // Set PUT-Data $data = "Data 0815"; $fh = tmpfile(); // Open tmp-File fwrite($fh, $data); // Save data fseek($fh, 0); // Set seek to begin curl_setopt($ch, CURLOPT_INFILE, $fh); curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data)); // Execute request $response = curl_exec($ch); echo "---- PUT ----\nStatus-Code: ". curl_getinfo($ch, CURLINFO_HTTP_CODE)."\n"; echo $response."\n----\n"; fclose($fh); curl_close($ch); // DELETE method // init curl $ch = curl_init(); // Set options for DELETE curl_setopt($ch, CURLOPT_URL, $url."/42"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); // Execute request $response = curl_exec($ch); echo "---- DELETE ----\nStatus-Code: ". curl_getinfo($ch, CURLINFO_HTTP_CODE)."\n"; echo $response."\n----\n"; curl_close($ch); ?> </code> server.php: <code> <?php // If GET method if($_SERVER['REQUEST_METHOD'] == 'GET'){ // First path element is id $path = $_SERVER['PATH_INFO']; if($path != null){
    $path_params = explode("/", $path);
} echo "Data of ID ".$path_params[1]; return; } // If POST method if($_SERVER['REQUEST_METHOD'] == 'POST'){ // Get POST-Body $body = file_get_contents("php://input"); // Set status header("HTTP/1.0 201 Created"); echo "New data: ".$body; return; } // If PUT method if($_SERVER['REQUEST_METHOD'] == 'PUT'){ // First path element is id $path = $_SERVER['PATH_INFO']; if($path != null){
    $path_params = explode("/", $path);
} // Get PUT-Body $body = file_get_contents("php://input"); // Status setzen header("HTTP/1.0 200 OK"); return; } // If DELETE method if($_SERVER['REQUEST_METHOD'] == 'DELETE'){ // First path element is id $path = $_SERVER['PATH_INFO']; if($path != null){
    $path_params = explode("/", $path);
} // Set status header("HTTP/1.0 200 OK"); return; } // Set status header("HTTP/1.0 501 Not Implemented"); ?> </code>

« previous php.test (#920) next »