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>’;

时间: 2024-12-31 00:52:43

PHP curl 实现RESTful PUT DELETE 实例的相关文章

php curl 伪造IP来源的实例代码

curl发出请求的文件fake_ip.php: 代码 代码如下: <?php $ch = curl_init(); $url = "http://localhost/target_ip.php"; $header = array( 'CLIENT-IP:58.68.44.61', 'X-FORWARDED-FOR:58.68.44.61', ); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPH

curl 命令大全及常用实例

一,curl命令参数 -a/--append 上传文件时,附加到目标文件 -A/--user-agent <string>  设置用户代理发送给服务器 - anyauth   可以使用"任何"身份验证方法 -b/--cookie <name=string/file> cookie字符串或文件读取位置 - basic 使用HTTP基本验证 -B/--use-ascii 使用ASCII /文本传输 -c/--cookie-jar <file> 操作结束后

RESTful WebService 入门实例

  /* 新建MavenProject,使用以下代码,创建类和POM文件.使用命令行切换到Project根目录,运行mvn package , mvn exec:java,即可启动RESTful service.在浏览器中输入http://localhost:8080/myapp/myresource/,此时会显示内容: Got it! ---Java RESTful Web Service 实战 */ package my.restful; 2 3 import org.glassfish.g

cURL的几个经典实例

1.cURL请求的基本步骤: (1)初始化 (2)设置选项,包括URL (3)执行并获取HTML文档内容 (4)释放cURL句柄 1 <?php 2 //1.初始化 3 $ch = curl_init(); 4 5 //2.设置选项,包括URL 6 curl_setopt($ch, CURLOPT_URL, "http://www.cnblogs.com/it-cen/"); 7 8 //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出 9 curl_seto

yii2 restful api delete的特殊处理

参考文章:http://www.digpage.com/request.html

openstack:RESTful API的查看与调试

1 相关文档及官网示例 官网API介绍: <openstack-bk-api-ref.pdf> 网址: http://developer.openstack.org/api-ref.html 下载pdf: http://developer.openstack.org/api-ref-guides/bk-api-ref.pdf 关于RESTful可以参考官方文档: http://docs.openstack.org/zh_CN/api/quick-start/content/index.html

WCF4.0 –- RESTful WCF Services (1) (入门)

WCF 很好的支持了 REST 的开发, 而 RESTful 的服务通常是架构层面上的考虑. 因为它天生就具有很好的跨平台跨语言的集成能力,几乎所有的语言和网络平台都支持 HTTP 请求,无需去实现复杂的客户端代理,无需使用复杂的数据通讯方式既可以将我们的服务暴露给任何需要的人,无论他使用 VB.Ruby.JavaScript,甚至是 HTML FORM,或者直接在浏览器地址栏输入. WCF 中通过 WebGetAttribute.WebInvokeAttribute (GET/PUT/POST

SQL[连载2]语法及相关实例

SQL语法 数据库表 一个数据库通常包含一个或多个表.每个表由一个名字标识(例如:"Websites"),表包含带有数据的记录(行). 在本教程中,我们在 MySQL 的 RUNOOB 数据库中中创建了 Websites 表,由于存储网站记录. 我们可以通过以下命令查看 "Websites" 表的数据: mysql> use RUNOOB; Database changed mysql> set names utf8; Query OK, 0 rows

《CURL技术知识教程》系列技术文章整理收藏

<CURL技术知识教程>系列技术文章整理收藏 1PHP采集相关教程之一 CURL函数库 2php中通过curl模拟登陆discuz论坛的实现代码 3php中通过curl smtp发送邮件 4PHP curl 并发最佳实践代码分享 5CURL的学习和应用(附多线程实现) 6php curl模仿ftp文件上传代码 7php使用curl来获取远程图片 8PHP Curl多线程原理实例详解 9curl不使用文件存取cookie php使用curl获取cookie示例 10php使用curl抓取qq空间