PHP curl

  1. 作者QQ:67065435 QQ群:821635552

  2. 本站内容全部为作者原创,转载请注明出处!

  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

    <?php
    $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

    <?php
    $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

    <?php
    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

    <?php
    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上传文件

    <?php
    $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保存文件

    <?php
    $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. 将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);
    
  11. 伪造客户端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;
    
  12. curl同时发起多个请求

    <?php
    function curl_multi($urls)
    {
        //请求集合
        $curls = [];
    
        //响应集合
        $result = [];
    
        //创建句柄
        $mh = curl_multi_init();
    
        //创建请求
        foreach ($urls as $url) {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
            curl_multi_add_handle($mh, $curl);
            $curls[] = $curl;
        }
    
        //执行句柄
        $running = null;
        do {
            //挂起线程
            usleep(100);
            curl_multi_exec($mh, $running);
        } while ($running > 0);
    
        //关闭句柄
        foreach ($curls as $curl) {
            $result[] = curl_multi_getcontent($curl);
            curl_multi_remove_handle($mh, $curl);
        }
        curl_multi_close($mh);
    
        //返回结果
        return $result;
    }
    
    //使用测试
    $urls = [
        'https://www.baidu.com/s?wd=0',
        'https://www.baidu.com/s?wd=1',
        'https://www.baidu.com/s?wd=2',
        'https://www.baidu.com/s?wd=3',
        'https://www.baidu.com/s?wd=4',
        'https://www.baidu.com/s?wd=5',
        'https://www.baidu.com/s?wd=6',
        'https://www.baidu.com/s?wd=7',
        'https://www.baidu.com/s?wd=8',
        'https://www.baidu.com/s?wd=9',
    ];
    $result = curl_multi($urls);
    file_put_contents(__DIR__ . "/test.json", json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    
  13. curl实现贴吧一键签到

    <?php
    /**
     * 从网页登录百度,从COOKIE中获取你的BDUSS填到这里
     */
    $BDUSS = "";//自己手填$_COOKIE['BDUSS']吧
    $header = [
        "Host: tieba.baidu.com",
        "X-Forwarded-For: 115.28.1." . mt_rand(1, 255),
        "Cookie: BDUSS={$BDUSS}; STOKEN=6502d329ec179796ae9badf548935c2c8e7b526e7e2ba54222267c635fa346a0",
        "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3775.400 QQBrowser/10.6.4208.400",
    ];
    
    /**
     * 1获取贴吧ID
     */
    $url = "http://tieba.baidu.com/f/user/json_userinfo";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    $data1 = curl_exec($ch);
    curl_close($ch);
    $data1 = json_decode($data1, true);
    if (empty($data1['data']['user_portrait'])) {
        exit(0);
    }
    
    /**
     * 2获取贴吧列表
     */
    $get = [
        "id" => $data1['data']['user_portrait'],
    ];
    $url = "https://tieba.baidu.com/home/main?" . http_build_query($get);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    $data2 = curl_exec($ch);
    curl_close($ch);
    preg_match_all('/<div class="clearfix u-f-wrap" id="forum_group_wrap">(.*?)<\/div>/', $data2, $matches);
    $data2 = $matches[0];
    preg_match_all('/<span>(.*?)<\/span>/', $data2[0], $matches);
    $data2 = $matches[1];
    
    /**
     * 3所有贴吧签到(客户端)
     */
    foreach ($data2 as $tieba) {
        //获取fid
        $get = [
            "ie" => "utf-8",
            "fname" => $tieba,
        ];
        $url = "http://tieba.baidu.com/f/commit/share/fnameShareApi?" . http_build_query($get);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        $data3 = curl_exec($ch);
        curl_close($ch);
        $data3 = json_decode($data3, true);
        if (!isset($data3['data']['fid'])) {
            continue;
        }
        //获取tbs
        $url = "http://tieba.baidu.com/dc/common/tbs";
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        $data4 = curl_exec($ch);
        curl_close($ch);
        $data4 = json_decode($data4, true);
        if (!isset($data4['tbs'])) {
            continue;
        }
        //贴吧签到
        $header1 = [
            "User-Agent: Fucking iPhone/1.0 BadApple/99.1",
            "Content-Type: application/x-www-form-urlencoded",
            "Cookie: BDUSS={$BDUSS}; STOKEN=6502d329ec179796ae9badf548935c2c8e7b526e7e2ba54222267c635fa346a0",
        ];
        $post = [
            'BDUSS' => $BDUSS,
            "_client_id" => "03-00-DA-59-05-00-72-96-06-00-01-00-04-00-4C-43-01-00-34-F4-02-00-BC-25-09-00-4E-36",
            "_client_type" => "4",
            "_client_version" => "1.2.1.17",
            "_phone_imei" => "540b43b59d21b7a4824e1fd31b08e9a6",
            'fid' => $data3['data']['fid'],
            'kw' => $tieba,
            'net_type' => '3',
            'tbs' => $data4['tbs'],
        ];
        //签名post
        $s = "";
        foreach ($post as $k => $v) {
            $s .= "{$k}={$v}";
        }
        $post['sign'] = strtoupper(md5("{$s}tiebaclient!!!"));
        $url = "http://c.tieba.baidu.com/c/c/forum/sign";
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        $data5 = curl_exec($ch);
        curl_close($ch);
        $data5 = json_decode($data5, true);
        if (isset($data5['error_msg'])) {
            echo "【客户端】【{$tieba}】" . json_decode("[\"{$data5['error_msg']}\"]", true)[0];
        } else {
            echo "【客户端】【{$tieba}】吧签到成功";
        }
        if (php_sapi_name() == "cli") {
            echo PHP_EOL;
            //防止签到太快被阻止
            sleep(5);
        } else {
            echo "<br>" . PHP_EOL;
        }
    }
    
    //客户端签到分数比网页高,先尝试进行客户端签到,都签到完之后,再尝试通过网页签到
    
    /**
     * 4所有贴吧签到(网页版)
     */
    foreach ($data2 as $tieba) {
        $post = [
            "ie" => "utf-8",
            "kw" => $tieba,
        ];
        $url = "http://tieba.baidu.com/sign/add";
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        $data3 = curl_exec($ch);
        curl_close($ch);
        $data3 = json_decode($data3, true);
        if (isset($data3['no']) && $data3['no'] !== 0) {
            echo "【网页版】【{$tieba}】" . json_decode("[\"{$data3['error']}\"]", true)[0];
        } else {
            echo "【网页版】【{$tieba}】吧签到成功";
        }
        if (php_sapi_name() == "cli") {
            echo PHP_EOL;
            //防止签到太快被阻止
            sleep(5);
        } else {
            echo "<br>" . PHP_EOL;
        }
    }
    

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

Copyright © 豆包嘿嘿~ 2012-∞ 冀ICP备17033181号 all right reserved,powered by Gitbook修订: 2020-08-28 15:52:10

results matching ""

    No results matching ""