note 107263 added to function.curl-exec
| From: | JohnSmith | Date: | Wed, 25 Jan 2012 20:47:00 +0000 |
| Subject: | note 107263 added to function.curl-exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185744@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>
----
Server IP: 85.214.154.209
Probable Submitter: 91.10.66.41
----
Manual Page -- http://www.php.net/manual/en/function.curl-exec.php
Edit -- https://master.php.net/note/edit/107263
Del: integrated -- https://master.php.net/note/delete/107263/integrated
Del: useless -- https://master.php.net/note/delete/107263/useless
Del: bad code -- https://master.php.net/note/delete/107263/bad+code
Del: spam -- https://master.php.net/note/delete/107263/spam
Del: non-english -- https://master.php.net/note/delete/107263/non-english
Del: in docs -- https://master.php.net/note/delete/107263/in+docs
Del: other reasons-- https://master.php.net/note/delete/107263
Reject -- https://master.php.net/note/reject/107263
Search -- https://master.php.net/manage/user-notes.php