PHP curl 实现RESTful PUT DELETE 实例
Posted 雨良
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP curl 实现RESTful PUT DELETE 实例相关的知识,希望对你有一定的参考价值。
客户端
client.php
<?php
//PUT
$curl_handle = curl_init ();
// Set default options.
curl_setopt ( $curl_handle, CURLOPT_URL, ‘http://my.focus.cn/test/socket.php’);
curl_setopt ( $curl_handle, CURLOPT_FILETIME, true );
curl_setopt ( $curl_handle, CURLOPT_FRESH_CONNECT, false );
curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
curl_setopt ( $curl_handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $curl_handle, CURLOPT_TIMEOUT, 5184000 );
curl_setopt ( $curl_handle, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $curl_handle, CURLOPT_NOSIGNAL, true );
curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, ‘PUT’ );
$aHeader[] = “Content-Type:text/xml;charset=UTF-8″;
$aHeader[] = “x-bs-ad:private”;
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $aHeader);
$file = ‘client.php’;
$file_size = filesize($file);
$h = fopen($file,’r‘);
curl_setopt ( $curl_handle, CURLOPT_INFILESIZE, $file_size);
curl_setopt ( $curl_handle, CURLOPT_INFILE, $h);
curl_setopt ( $curl_handle, CURLOPT_UPLOAD, true );
$ret = curl_exec ( $curl_handle );
print_r($ret);
?>
DELETE 只要将
$curl_handle = curl_init ();
// Set default options.
curl_setopt ( $curl_handle, CURLOPT_URL, ‘http://my.focus.cn/test/socket.php?file=socket.txt’);
curl_setopt ( $curl_handle, CURLOPT_FILETIME, true );
curl_setopt ( $curl_handle, CURLOPT_FRESH_CONNECT, false );
curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
curl_setopt ( $curl_handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $curl_handle, CURLOPT_TIMEOUT, 5184000 );
curl_setopt ( $curl_handle, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $curl_handle, CURLOPT_NOSIGNAL, true );
curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, ‘DELETE’ );
$ret = curl_exec ( $curl_handle );
服务端
server.php
<?php
$raw_post_data = file_get_contents(‘php://input’, ‘r’);
$method = $_SERVER[‘REQUEST_METHOD‘];
if(‘PUT’ == $method)
{
$headers = apache_request_headers();
file_put_contents(‘socket.txt’,$raw_post_data.print_r($headers,true));
}
else if(‘DELETE’==$method)
{
unlink($_GET[‘file‘]);
}
echo ‘<?xml version=”1.0″ encoding=”UTF-8″?><RET>OK</RET>’;
客户端
client.php
<?php
//PUT
$curl_handle = curl_init ();
// Set default options.
curl_setopt ( $curl_handle, CURLOPT_URL, ‘http://my.focus.cn/test/socket.php’);
curl_setopt ( $curl_handle, CURLOPT_FILETIME, true );
curl_setopt ( $curl_handle, CURLOPT_FRESH_CONNECT, false );
curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
curl_setopt ( $curl_handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $curl_handle, CURLOPT_TIMEOUT, 5184000 );
curl_setopt ( $curl_handle, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $curl_handle, CURLOPT_NOSIGNAL, true );
curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, ‘PUT’ );
$aHeader[] = “Content-Type:text/xml;charset=UTF-8″;
$aHeader[] = “x-bs-ad:private”;
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $aHeader);
$file = ‘client.php’;
$file_size = filesize($file);
$h = fopen($file,’r‘);
curl_setopt ( $curl_handle, CURLOPT_INFILESIZE, $file_size);
curl_setopt ( $curl_handle, CURLOPT_INFILE, $h);
curl_setopt ( $curl_handle, CURLOPT_UPLOAD, true );
$ret = curl_exec ( $curl_handle );
print_r($ret);
?>
DELETE 只要将
$curl_handle = curl_init ();
// Set default options.
curl_setopt ( $curl_handle, CURLOPT_URL, ‘http://my.focus.cn/test/socket.php?file=socket.txt’);
curl_setopt ( $curl_handle, CURLOPT_FILETIME, true );
curl_setopt ( $curl_handle, CURLOPT_FRESH_CONNECT, false );
curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
curl_setopt ( $curl_handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $curl_handle, CURLOPT_TIMEOUT, 5184000 );
curl_setopt ( $curl_handle, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $curl_handle, CURLOPT_NOSIGNAL, true );
curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, ‘DELETE’ );
$ret = curl_exec ( $curl_handle );
服务端
server.php
<?php
$raw_post_data = file_get_contents(‘php://input’, ‘r’);
$method = $_SERVER[‘REQUEST_METHOD‘];
if(‘PUT’ == $method)
{
$headers = apache_request_headers();
file_put_contents(‘socket.txt’,$raw_post_data.print_r($headers,true));
}
else if(‘DELETE’==$method)
{
unlink($_GET[‘file‘]);
}
echo ‘<?xml version=”1.0″ encoding=”UTF-8″?><RET>OK</RET>’;
以上是关于PHP curl 实现RESTful PUT DELETE 实例的主要内容,如果未能解决你的问题,请参考以下文章