PHP curl

  1. 作者QQ:67065435 QQ群:669756510

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

  3. 扩展需求

    curl
    
  4. 基本格式

    <?php
    $url = "https://www.test.com/?" . http_build_query([
        "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",
    ];
    $method = "POST";
    $encode = "gzip,deflate";
    $dns = ["www.test.com:443:127.0.0.1"];
    $cookie = sys_get_temp_dir() . "/cookie";
    if (!is_file($cookie)) {
        touch($cookie);
    }
    $proxy_host = "127.0.0.1";
    $proxy_port = "10809";
    $proxy_user = "12345@qq.com";
    $proxy_pswd = "Aa1234567890";
    $ch = curl_init();
    
    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_URL, $url);                    //声明要请求的url
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);            //传递POST数据体到服务端
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, $head);            //传递HEAD请求头到服务端
    
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);       //请求方式规定为POST
    
    curl_setopt($ch, CURLOPT_ENCODING, $encode);            //响应数据乱码解决
    
    curl_setopt($ch, CURLOPT_RESOLVE, $dns);                //本地进行DNS解析
    
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);           //保存cookie数据的文件路径
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);          //读取cookie数据的文件路径
    
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);                //通过代理进行请求
    curl_setopt($ch, CURLOPT_PROXY, $proxy_host);                       //通过代理进行请求
    curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);                   //通过代理进行请求
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$proxy_user:$proxy_pswd");  //通过代理进行请求
    
    $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. 附录

    <?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]+/";
    
Copyright © 鲸小鱼 2012-∞ all right reserved,powered by Gitbook修订: 2000-10-10 00:00

results matching ""

    No results matching ""