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. Android--自定义加载框

    1,在网上看了下好看的加载框,看了一下,挺好看的,再看了下源码,就是纯paint画出来的,再加上属性动画就搞定了 再来看一下我们的源码 LvGhost.java package com.qianmo. ...

  2. 找出数组中从未出现的最小正整数java实现

    /** * 找出未出现的最小正整数 * @param A * @param n * @date 2016-10-7 * @author shaobn */ public static int find ...

  3. 华盛顿大学 Programming Languages

    表达式三要素: 语法,类型和计算值.

  4. Djunit工作记录

    1.下载djunit 解压后放置到eclipse的plugins目录下即可 2.测试程序必须继承DjunitTestCase 3.测试程序完run as DJunitTest 运行 4.在djunit ...

  5. 9款一键快速搭建PHP运行环境的好工具

    9款一键快速搭建PHP运行环境的好工具 胡倡萌 2011/02/19 网络资源 77,063 1     内容提要: 建立一个PHP网站,首先需要搭建PHP的开发和运行环境,对于PHP初学者也是一个难 ...

  6. python学习笔记系列----(八)python常用的标准库

    终于学到了python手册的最后一部分:常用标准库.这部分内容主要就是介绍了一些基础的常用的基础库,可以大概了解下,在以后真正使用的时候也能想起来再拿出来用. 8.1 操作系统接口模块:OS OS模块 ...

  7. JS SDK 随手笔记

    JS SDK 随手笔记 窗口模块 Frame/Multi Frame 对话框 页面间的通讯 生命周期 窗口层叠 窗口模块 窗口模块是是AppCan移动应用界面最基本的单位.窗口是每个界面布局的基础,他 ...

  8. winApi

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  9. margin padding

    margin and padding有时候可以实现相同的效果但是 用padding的场合更多 2者的区别和用法的细节我还是没掌握好

  10. C语言共用体(Union)

    通过前面的讲解,我们知道结构体(Struct)是一种构造类型或复杂类型,它可以包含多个类型不同的成员.在C语言中,还有另外一种和结构体非常类似的语法,叫做共用体(Union),它的定义格式为: uni ...