php curl put delete,PHP使用CURL模拟POST/GET/PUT/DELETE方式提交数据

最近因为工作需要,调用网盘接口来上传文件,我用了CURL库, 当然在用CURL库之前必须要在php中启用 cURL 设置

可以通过使用php_info()函数来得到cURL信息,如果看不到cURL信息的话,那么需要设置PHP并开启这个库。在Windows平台下,需要改一改php.ini文件的设置,找到    php_curl.dll,并取消前面的分号注释就行了。

一般的文件上传是通过html表单进行的,通过CURL可以不经过浏览器,直接在服务器端模拟进行表单提交,完成POST数据、文件上传等功能。

我们是可以通过其他办法获取网页内容。大多数时候,我因为想偷懒,都直接用简单的PHP函数,如下:

$content = file_get_contents("http://www.doucube.com");

// or

$lines = file("http://www.doucube.com");

// or

readfile(http://www.doucube.com);

不过,这种做法缺乏灵活性和有效的错误处理。而且,你也不能用它完成一些高难度任务——比如处理coockies、验证、表单提交、文件上传等等。所以选择curl库。

示例代码:

$url = 'https://www.google.com';

$method = 'POST';

//headers and data (this is API dependent, some uses XML):

//即在接口调用时才用headers 和$data

$headers = array(

'Accept: application/json',

'Content-Type: application/json',

);

$data = json_encode(array(

'firstName'=> 'John',

'lastName'=> 'Doe'

));

// 启动一个CURL会话

$handle = curl_init();

curl_setopt($handle, CURLOPT_URL, $url); // 要访问的地址

curl_setopt($handle,CURLOPT_HEADER,1); // 是否显示返回的Header区域内容

curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); //设置请求头

curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回

curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在

curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查

switch($method) {

case 'GET':

break;

case 'POST':

curl_setopt($handle, CURLOPT_POST, true);

curl_setopt($handle, CURLOPT_POSTFIELDS, $data); //设置请求体,提交数据包

break;

case 'PUT':

curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');

curl_setopt($handle, CURLOPT_POSTFIELDS, $data); //设置请求体,提交数据包

break;

case 'DELETE':

curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');

break;

}

$response = curl_exec($handle); // 执行操作

$code = curl_getinfo($handle, CURLINFO_HTTP_CODE); // 获取返回的状态码

curl_close ($handle); // 关闭CURL会话

if('200'==$code){

echo "ok";

}

下面还有一个示例,有兴趣可以看看:

用curl上传文件的话很方便,什么header,post串都不用生成了,用fsockopen要写一堆 curl: ==============

PHP code

$file = array("upimg"=>"@E:/png.png");//文件路径,前面要加@,表明是文件上传.

$curl = curl_init("http://localhost/a.php");

curl_setopt($curl,CURLOPT_POST,true);

curl_setopt($curl,CURLOPT_POSTFIELDS,$file);

curl_exec($curl);

fsockopen: ===============

PHP code $uploadFile = file_get_contents("E:/png.png"); $boundary   = md5(time());

$postStr .="--".$boundary."\r\n";//边界开始,注意默认比header定义的boundary多两个'-'

$postStr .="Content-Disposition: form-data; name=\"upimg\"; filename=\"E:/png.png\"\r\n";

$postStr .="Content-Type: image/png\r\n\r\n";

$postStr .=$uploadFile."\r\n"; $postStr .="--".$boundary."\r\n";//边界结束

write($fp,"POST /a.php HTTP/1.0\r\n");

fwrite($fp,"Content-Type: multipart/form-data; boundary=".$boundary."\r\n");

fwrite($fp,"Content-length:".strlen($postStr)."\r\n\r\n");

fwrite($fp,$postStr);

while (!feof($fp))

{

echo fgets($fp, 128);

}

fclose($fp);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值