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

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

  1. 【转】php curl 伪造IP来源的实例代码

    curl发出请求的文件fake_ip.php: 代码 复制代码 代码如下: <?php $ch = curl_init(); $url = "http://localhost/targ ...

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

    来源:http://www.jb51.net/article/31694.htm curl 它不但可以模仿用户登录,还可以模仿用户IP地址哦,为伪造IP来源,本实例仅供参考哦 //伪造ip ; $i ...

  3. PHP中使用CURL模拟文件上传实例

    调用实例: 该方法将本地的E盘文件test.doc上传到接口服务器上的 uploadFile方法中,uploadFile会对上传的文件做进一步处理. 若你想自己对上传的文件做操作,将接口uploadF ...

  4. 比 file_get_contents() 更优的 cURL 详解(附实例)

    PHP 可以使用 file_get_content() 函数抓取网页内容,但却无法进行更复杂的处理,譬如文件的上传或下载. Cookie 操作等等.而 cURL 提供了这些功能. 一.cURL简介 在 ...

  5. SpringMVC(三)Restful风格及实例、参数的转换

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Restful风格 1.Restful风格的介绍 Restful 一种软件架构风格.设计风格,而不是 ...

  6. cURL的几个经典实例

    1.cURL请求的基本步骤: (1)初始化 (2)设置选项,包括URL (3)执行并获取HTML文档内容 (4)释放cURL句柄 <?php //1.初始化 $ch = curl_init(); ...

  7. RESTful WebService 入门实例

      /* 新建MavenProject,使用以下代码,创建类和POM文件.使用命令行切换到Project根目录,运行mvn package(或者,选中pom.xml 文件右键单击 > run a ...

  8. Spring4 MVC RESTFul WebServices CRUD实例+RestTemplate

    简单介绍REST REST 表示状态传输.这是一个体系结构样式,可用于设计网络服务,可以被各种客户端消耗.核心思想是,不使用如CORBA,RPC或SOAP复杂的机制在机器之间进行连接,简单的 HTTP ...

  9. RESTFul Client入门实例

    client.html文件内容为: <!DOCTYPE html> <html> <head> <title>RESTFul Client test p ...

随机推荐

  1. 摘抄转载前辈们的Java集合类总结

    本文摘自 Blue Sky:http://www.cnblogs.com/hubcarl JAVA 集合类介绍和使用 类关系示意图Iterable(接口) │ └--Collection (接口) ├ ...

  2. 收集一些关于OI/ACM的奇怪的东西……

    一.代码: 1.求逆元(原理貌似就是拓展欧几里得,要求MOD是素数): int inv(int a) { if(a == 1) return 1; return ((MOD - MOD / a) * ...

  3. Hdu 2955 Robberies 0/1背包

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. linux搭建微型git服务器

    1.安装git和git-core yum install git git-core -y 2.创建仓库 mkdir /home/git cd /home/git git init 3.设置可以远程pu ...

  5. C/C++之指针加减法

    C和C++中可对指针进行加减,但对其进行乘除则基本无实际意义.一般来说,对指针进行减法的前提是减数和被减数均指向同一数组.加法同理.需要注意的是,两个指针的减法,结果是两个地址之间索引变量的数目,而不 ...

  6. 10分钟学习pandas

    10 Minutes to pandas This is a short introduction to pandas, geared mainly for new users. You can se ...

  7. 终端更新ubuntu系统

    1.sudo apt-get update 2. sudo apt-get dist-upgrade

  8. next([expr]) 取得一个包含匹配的元素集合中每一个元素紧邻的后面同辈元素的元素集合。

    描述: 找到每个段落的后面紧邻的同辈元素. HTML 代码: <p>Hello</p><p>Hello Again</p><div>< ...

  9. spring " expected single matching bean but found 2" 问题一例。

    初入java,使用spring时遇到一个问题,左边是一个接口和实现.右边是service和实现. @Service@Transactional(rollbackFor = Exception.clas ...

  10. Android 获取渠道名称

    直接看代码, //获取渠道名称public static String getChannelName(Activity ctx) { if (ctx == null) { return null; } ...