PHP curl

  1. 作者QQ:67065435 QQ群:821635552

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

  3. 扩展需求

    curl
    
  4. 基本格式1

    <?php
    $get = [
        "get1" => "get_val1",
        "get2" => "get_val2",
        "get3" => "get_val3",
    ];
    $body = [
        "body1" => "body_val1",
        "body2" => "body_val2",
        "body3" => "body_val3",
        "file1" => new CURLFile("/xxx/file1.gif", "image/gif", "file1.gif"),
        "file2" => new CURLFile("/xxx/file2.png", "image/png", "file2.png"),
        "file3" => new CURLFile("/xxx/file3.jpg", "image/jpeg", "file3.jpg"),
    ];
    $head = [
        "head1: head_val1",
        "head2: head_val2",
        "head3: head_val3",
    ];
    $url = "https://xxx.com/?" . http_build_query($get);
    $ch = curl_init($url);                              //引入url变量方式1
    curl_setopt($ch, CURLOPT_URL, $url);                //引入url变量方式2
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);        //跳过SSL安全验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);        //跳过SSL安全验证
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        //curl_exec返回数据
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);        //传递POST数据体到服务端
    curl_setopt($ch, CURLOPT_HTTPHEADER, $head);        //传递HEAD请求头到服务端
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");    //请求方式规定为POST
    $data = curl_exec($ch);                             //获取响应数据体
    $errno = curl_errno($ch);                           //获取错误编号
    $error = curl_error($ch);                           //获取错误信息
    curl_close($ch);                                    //关闭请求连接
    /**
     * 注意事项
     * 1. 文件请求 $body array                                  Content_type: multipart/form-data
     * 2. 普通请求 $body = http_build_query($body);             Content_type: application/x-www-form-urlencoded
     * 3. JSON请求 $body = json_encode($body);                 Content_type: application/json
     * 4. XML请求  $body = <?xml version="1.0"?>xxx</xml>      Content_type: application/xml
     * 5. And so on...
     */
    
  5. 基本格式2

    <?php
    $get = [
        "get1" => "get_val1",
        "get2" => "get_val2",
        "get3" => "get_val3",
    ];
    $body = [
        "body1" => "body_val1",
        "body2" => "body_val2",
        "body3" => "body_val3",
        "file1" => new CURLFile("/xxx/file1.gif", "image/gif", "file1.gif"),
        "file2" => new CURLFile("/xxx/file2.png", "image/png", "file2.png"),
        "file3" => new CURLFile("/xxx/file3.jpg", "image/jpeg", "file3.jpg"),
    ];
    $head = [
        "head1: head_val1",
        "head2: head_val2",
        "head3: head_val3",
    ];
    $cookie = sys_get_temp_dir() . "/cookie";
    if (!is_file($cookie)) {
        touch($cookie);
    }
    $url = "https://xxx.com/?" . http_build_query($get);
    $ch = curl_init($url);//引入url变量方式1
    curl_setopt($ch, CURLOPT_URL, $url);                //引入url变量方式2
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);        //跳过SSL安全验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);        //跳过SSL安全验证
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        //curl_exec返回数据
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);        //传递POST数据体到服务端
    curl_setopt($ch, CURLOPT_HTTPHEADER, $head);        //传递HEAD请求头到服务端
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");    //请求方式规定为POST
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);       //响应cookie保存进来
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);      //请求cookie携带过去
    $data = curl_exec($ch);                             //获取响应数据体
    $errno = curl_errno($ch);                           //获取错误编号
    $error = curl_error($ch);                           //获取错误信息
    curl_close($ch);                                    //关闭请求连接
    /**
     * 注意事项
     * 1. 文件请求 $body array                                  Content_type: multipart/form-data
     * 2. 普通请求 $body = http_build_query($body);             Content_type: application/x-www-form-urlencoded
     * 3. JSON请求 $body = json_encode($body);                 Content_type: application/json
     * 4. XML请求  $body = <?xml version="1.0"?>xxx</xml>      Content_type: application/xml
     * 5. And so on...
     */
    
  6. 基本格式2-附录

    <?php
    //上文$cookie指定了cookie文件位置为:
    //sys_get_temp_dir() . "/cookie"
    //但是未介绍cookie中数据的存储格式
    //每一个cookie在文件中,格式如下所示:
    $domain = "xxx.com";        //作用域
    $subdomain = "FALSE";       //false-不含子域    true-包含子域
    $path = "/";                //作用路径
    $secure = "FALSE";          //false-无须SSL传输 true-必须SSL传输
    $expire = time() + 86400;   //过期时间
    $name = "name";             //键
    $value = "value";           //值
    $cookie_str = <<<EOF
    {$domain}    {$subdomain}    {$path}    {$secure}    {$expire}    {$name}    {$value}
    {$domain}    {$subdomain}    {$path}    {$secure}    {$expire}    {$name}    {$value}
    {$domain}    {$subdomain}    {$path}    {$secure}    {$expire}    {$name}    {$value}
    EOF;
    //每一个cookie在文件中,用正则表达式这样匹配
    $pattern = "/([^\t]+\t){6}[^\t]+/";
    
  7. curl同时发起多个请求

    <?php
    function curl_multi($urls)
    {
        $mh = curl_multi_init();
    
        $chs = [];
        foreach ($urls as $url) {
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_multi_add_handle($mh, $ch);
            $chs[] = $ch;
        }
    
        $running = null;
        do {
            curl_multi_exec($mh, $running);
        } while ($running > 0);
    
        $result = [];
        foreach ($chs as $ch) {
            $result[] = curl_multi_getcontent($ch);
            curl_multi_remove_handle($mh, $ch);
        }
        curl_multi_close($mh);
    
        return $result;
    }
    
    //使用测试
    $urls = [
        "https://xxx.com/clear?key=鬼吹灯0",
        "https://xxx.com/clear?key=鬼吹灯1",
        "https://xxx.com/clear?key=鬼吹灯2",
        "https://xxx.com/clear?key=鬼吹灯3",
        "https://xxx.com/clear?key=鬼吹灯4",
        "https://xxx.com/clear?key=鬼吹灯5",
        "https://xxx.com/clear?key=鬼吹灯6",
        "https://xxx.com/clear?key=鬼吹灯7",
        "https://xxx.com/clear?key=鬼吹灯8",
        "https://xxx.com/clear?key=鬼吹灯9",
    ];
    $result = curl_multi($urls);
    file_put_contents(__DIR__ . "/test.json", json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    
Copyright © 豆包嘿嘿~ 2012-∞ 冀ICP备17033181号 all right reserved,powered by Gitbook修订: 21-07-20 14:39

results matching ""

    No results matching ""