test param:1103
| From: | Mai Levesque | Date: | Wed, 25 Jan 2012 20:51:23 +0000 |
| Subject: | test param:1103 | ||
| Groups: | php.test | ||
| Request: | Send a blank email to php-test+get-919@lists.php.net to get a copy of this message | ||
RESTful Service
--------------------------------------------------------------------------------
Client
--------------------------------------------------------------------------------
<?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);
?>
--------------------------------------------------------------------------------
Server
--------------------------------------------------------------------------------
<?php
class mysqldb {
private $db;
public function __construct() {
// Connect to mysql server and select database
try {
$this->db = new PDO ("mysql:host=localhost;dbname=database","user","password");
} catch (PDOException $e) {
throw new Exception ('Connection failed: ' .$e->getMessage());
}
}
public function getMessage ($id) {
// Get message from news
$a = array();
if (!$stmt=$this->db->prepare('SELECT message FROM news WHERE id=:id'))
throw new Exception ('PDOStatement::prepare failed!');
if (!$stmt->bindParam(':id',$id))
throw new Exception ('PDOStatement::bindParam failed!');
if (!$stmt->execute())
throw new Exception ('PDOStatement::execute failed!');
// Save data to an array
while($news=$stmt->fetchObject()) {
array_push($a, $news->message);
}
// Return result
return ($a);
}
public function insertMessage ($message) {
// Insert message into news
if (!$stmt=$this->db->prepare('INSERT INTO news (time, message) VALUES (CURRENT_TIMESTAMP,:message)'))
throw new Exception ('PDOStatement::prepare failed!');
if (!$stmt->bindParam(':message',$message))
throw new Exception ('PDOStatement::bindParam failed!');
if(!$stmt->execute())
throw new Exception ('PDOStatement::execute failed!');
}
public function updateMessage ($id,$message) {
// Update message in news
if (!$stmt=$this->db->prepare('UPDATE news SET message=:message WHERE id=:id'))
throw new Exception ('PDOStatement::prepare failed!');
if (!$stmt->bindParam(':id',$id)||!$stmt->bindParam(':message',$message))
throw new Exception ('PDOStatement::bindParam failed!');
if(!$stmt->execute())
throw new Exception ('PDOStatement::execute failed!');
}
public function deleteMessage ($id) {
// Delete message from news
if (!$stmt=$this->db->prepare('DELETE FROM news WHERE id=:id'))
throw new Exception ('PDOStatement::prepare failed!');
if (!$stmt->bindParam(':id',$id))
throw new Exception ('PDOStatement::bindParam failed!');
if(!$stmt->execute())
throw new Exception ('PDOStatement::execute failed!');
}
}
// 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 "Message ".$path_params[1].": ";
//Create new db instance
$mysql = new mysqldb();
//Get data
$output = array();
$output = $mysql->getMessage($path_params[1]);
reset($output);
while( key($output) !== NULL )
{
echo current($output);
next($output);
}
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 Message: ".$body;
//Create new db instance
$mysql = new mysqldb();
//Insert data
$mysql->insertMessage($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");
//Create new db instance
$mysql = new mysqldb();
//Updatet data
$mysql->updateMessage($path_params[1],$body);
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");
//Create new db instance
$mysql = new mysqldb();
//Delete data
$mysql->deleteMessage($path_params[1]);
return;
}
// Set status
header("HTTP/1.0 501 Not Implemented");
?>