PHP curl

  1. 常用方法

     # 初始化
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     # OR
     $ch = curl_init($url);
    
     # 返回响应头
     //最后的参数为0表示不返回响应头信息,为1表示返回响应头部信息
     curl_setopt($ch, CURLOPT_HEADER, 1);
    
     # 追踪请求头
     //最后的参数为0表示不追踪请求头文件,为1表示追踪请求头部信息
     curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
    
     //请求头携带设备类型
     $user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.19 Safari/537.36';
     curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    
     # 请求结果的处理
     //最后的参数为0表示curl_exec执行结果直接输出到浏览器,为1表示curl_exec返回执行结果
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
     # 请求方式定义
     //最后的参数为0表示以post的方式提交
     curl_setopt($ch, CURLOPT_POST, 1);
     //最后的参数直接定义请求的方式
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'post');
    
     # 设置post数据
     curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
    
     # 是否进行SSL认证
     # 最后的参数为0表示不进行安全认证,为1表示进行认证
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    
     # 请求、响应均携带cookie
     $cookie_file = __DIR__ . '/cookie.txt';
     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    
     # 声明获取到数据的压缩方式(gzip,deflate,sdch等)
     curl_setopt($ch, CURLOPT_ENCODING , 'gzip');
    
     # 去除响应头中的(gzip,deflate,sdch等)
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
    
     # 获取请求结果
     $data = curl_exec($ch);
    
     # 关闭curl请求
     curl_close($ch);
    
  2. curl_get

     $url = 'http://localhost/index.php';
     $ch = curl_init($url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     $data = curl_exec($ch);
     curl_close($ch);
     echo $data;
    
  3. curl_post

     $url = 'http://localhost/index.php';
     $array = [
         'id' => 1,
         'name' => 'dog'
     ];
     $ch = curl_init($url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
     $data = curl_exec($ch);
     curl_close($ch);
     echo $data;
    
  4. curl_get_header

     function curlGetHeader($url)
     {
         $curl = curl_init($url);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
         curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
         curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
         curl_setopt($curl, CURLOPT_HEADER, 1);//带头
         curl_setopt($curl, CURLINFO_HEADER_OUT, 1);//带头
         $result = [];
         $result['data'] = curl_exec($curl);
         $result['header'] = curl_getinfo($curl, CURLINFO_HEADER_OUT);//取头
         curl_close($curl);
         return $result;
     }
    
  5. curl_post_header

     function curlPostHeader($url, $post_arr)
     {
         $curl = curl_init($url);
         curl_setopt($curl, CURLOPT_POST, 1);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($curl, CURLOPT_POSTFIELDS, $post_arr);
         curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
         curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
         curl_setopt($curl, CURLOPT_HEADER, 1);//带头
         curl_setopt($curl, CURLINFO_HEADER_OUT, 1);//带头
         $result = [];
         $result['data'] = curl_exec($curl);
         $result['header'] = curl_getinfo($curl, CURLINFO_HEADER_OUT);//取头
         curl_close($curl);
         return $result;
     }
    
  6. curl上传文件

     $file_path = './123.gif';
     $file_name = explode('/',$file_path);
     $file_name = end($file_name);
     $finfo = new \finfo(FILEINFO_MIME_TYPE);
     $mime_type = $finfo->file($file_path);
     $url = 'http://localhost/add_file';
     $data = [
         'book_id' => 51,
         'book_cover' => new CURLFile($file_path, $mime_type, $file_name)
     ];
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
     $res = curl_exec($ch);
     curl_close($ch);
     echo $res;
    
  7. curl保存文件

     $path = "http://www.xxx.com/logo.png";
     $file_save_path = "C:\Users\user1\Desktop\logo.jpg";
     $ch = curl_init($path);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     $file_data = curl_exec($ch);
     curl_close($ch);
     file_put_contents($file_save_path, $file_data);
    
  8. curl同时发起多个请求(注意事项:不要尝试请求php-cli模式下生成的URL,否则不能并发执行)

     <?php
     function concurrent($url_list)
     {
         $mh = curl_multi_init();
         $ch_list = [];
         foreach ($url_list as $key => $url) {
             $ch = curl_init();
             curl_setopt($ch, CURLOPT_URL, $url);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
             curl_setopt($ch, CURLOPT_HEADER, 0);
             $ch_list[$key] = $ch;
             curl_multi_add_handle($mh, $ch);
         }
         do {
             curl_multi_exec($mh, $active);
             curl_multi_select($mh);
         } while ($active > 0);
         $res = [];
         foreach ($ch_list as $key => $ch) {
             $error = curl_error($ch);
             if (empty($error)) {
                 $res[$key] = curl_multi_getcontent($ch);
             }
             curl_close($ch);
             curl_multi_remove_handle($mh, $ch);
         }
         curl_multi_close($mh);
         return $res;
     }
    
     $url_list = [];
     $id_list = [66666, 66667, 66668, 66669, 66610, 66611, 66612];
     foreach ($id_list as $value) {
         $url_list[$value] = 'http://localhost/test.php?book_id=' . $value;
     }
     $res = concurrent($url_list);
     print_r($res);
    

curl_error 60

  1. 下载cacert.pem

     cd xxx/php/extras/ssl/
     wget https://curl.haxx.se/ca/cacert.pem
    
  2. 修改配置

     ;curl.cainfo=""
     改为
     curl.cainfo="xxx/php/extras/ssl/cacert.pem"
    
     ;openssl.cafile=""
     改为
     openssl.cafile="xxx/php/extras/ssl/cacert.pem"
    
  3. 重启php

@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2018-08-14 11:08:13

results matching ""

    No results matching ""