PHP curl

  1. 将cookie字符串写入PHP CURL的cookie文件

    <?php
    //假设以POST方式提交cookie字符串
    $cookie_str = $_POST['cookie'];
    //将要整理的cookie
    $cookie = "";
    //超时时间设置为30天
    $time = time() + 2592000;
    //cookie存储路径
    $cookie_file = __DIR__ . '/cookie.txt';
    //去除cookie字符串中的空格
    $cookie_str = str_replace(' ', '', $cookie_str);
    //将cookie按';'切分为数组
    $cookie_str = explode(';', $cookie_str);
    foreach ($cookie_str as $value) {
        //将每个cookie的键值进行urldecode解码
        $value = urldecode($value);
        //将每个cookie的键与cookie的值拆分出来
        $value = explode('=', $value);
        if (count($value) == 2) {
            //整理cookie
            $cookie .= ".weixin.qq.com\tTRUE\t/\tFALSE\t{$time}\t{$value[0]}\t{$value[1]}" . PHP_EOL;
        }
    }
    //将cookie存储到PHP CURL的cookie文件中
    file_put_contents($cookie_file, $cookie);
    
  2. 伪造客户端IP和代理IP

    <?php
    $url = "https://www.baidu.com";
    $curl = curl_init($url);
    $header = [
        'CLIENT-IP:114.114.114.114',
        'X-FORWARDED-FOR:114.114.114.114'
    ];
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 120);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_TIMEOUT, 120);
    $result = curl_exec($curl);
    curl_close($curl);
    echo $result;
    
  3. 常用方法

     # 初始化
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, $url);
     # 或者
     $curl = curl_init($url);
    
     # 返回响应头
     //最后的参数为0表示不打印响应头,为1表示直接打印响应头
     curl_setopt($curl, CURLOPT_HEADER, 1);
    
     # 追踪请求头
     //最后的参数为0表示不追踪请求头,为1表示追踪请求头
     curl_setopt($curl, 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($curl, CURLOPT_USERAGENT, $user_agent);
    
     # 请求结果的处理
     //最后的参数为0表示curl_exec打印输出,为1表示curl_exec返回结果
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    
     # 请求方式定义
     //最后的参数为1表示以POST的方式提交
     curl_setopt($curl, CURLOPT_POST, 1);
     //最后的参数直接定义请求的方式
     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
    
     # 设置POST数据
     curl_setopt($curl, CURLOPT_POSTFIELDS, $array);
    
     # 是否进行SSL认证
     # 最后的参数为0表示不进行安全认证,为1表示进行认证
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    
     # 请求、响应均携带cookie(仅限cli模式)
     $cookie_file = __DIR__ . '/cookie.txt';
     curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
     curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
    
     # 声明获取到数据的压缩方式(gzip,deflate,sdch等)
     curl_setopt($curl, CURLOPT_ENCODING , 'gzip');
    
     # 去除响应头中的(gzip,deflate,sdch等)
     curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
    
     # 获取请求结果
     $data = curl_exec($curl);
    
     # 关闭curl请求
     curl_close($curl);
    
  4. curl_get

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

     $url = 'http://localhost/index.php';
     $array = [
         'id' => 1,
         'name' => 'dog'
     ];
     $curl = curl_init($url);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($curl, CURLOPT_POSTFIELDS, $array);
     $data = curl_exec($curl);
     curl_close($curl);
     echo $data;
    
  6. 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;
     }
    
  7. curl_post_header

     function curlPostHeader($url, $post_arr)
     {
         $curl = curl_init($url);
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
         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;
     }
    
  8. 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)
     ];
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
     $res = curl_exec($curl);
     curl_close($curl);
     echo $res;
    
  9. curl保存文件

     $path = "http://www.xxx.com/logo.png";
     $file_save_path = "C:\Users\user1\Desktop\logo.jpg";
     $curl = curl_init($path);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
     $file_data = curl_exec($curl);
     curl_close($curl);
     file_put_contents($file_save_path, $file_data);
    
  10. curl同时发起多个请求

     <?php
     function concurrent($url_list)
     {
         $mh = curl_multi_init();
         $curl_list = [];
         foreach ($url_list as $key => $url) {
             $curl = curl_init();
             curl_setopt($curl, CURLOPT_URL, $url);
             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
             curl_setopt($curl, CURLOPT_HEADER, 0);
             $curl_list[$key] = $curl;
             curl_multi_add_handle($mh, $curl);
         }
         do {
             curl_multi_exec($mh, $active);
             curl_multi_select($mh);
         } while ($active > 0);
         $res = [];
         foreach ($curl_list as $key => $curl) {
             $error = curl_error($curl);
             if (empty($error)) {
                 $res[$key] = curl_multi_getcontent($curl);
             }
             curl_close($curl);
             curl_multi_remove_handle($mh, $curl);
         }
         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

results matching ""

    No results matching ""