PHP使用CURL模拟POST/GET/PUT/DELETE方式提交数据

2019-05-05 14:04:11

  最近因为工作需要,调用网盘接口来上传文件,我用了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库。 


示例代码:


<?php

    $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);



  • 2018-01-20 20:33:58

    PHP中session_cache_limiter() 是什么意思啊

    session_cache_limiter('public'); session_cache_expire(30); session_start();第三个我晓得 前两个 网络上没找着资料 手册上全是英文的 翻译后 还是看不懂主要是没翻译正确

  • 2018-01-23 02:22:17

    andorid,把控件置于最顶端。bringToFront的意外发现

     bringToFront方法在SDK中的说明是“Change the view's z order in the tree, so it's on top of other sibling views”,翻译过来就是改变view的z轴,使其处在他父view的顶端。关于bringToFront的实现,网上有很多资料介绍,大体来说就是将这个view从父view中移除,然后再加入父view的顶端。具体实现如何呢?