• PHP中curl的使用


    cURL 函数

    1、一个简单的例子

    <?php
    // 创建一个新cURL资源
    $ch = curl_init();
    
    // 设置URL和相应的选项
    curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
    curl_setopt($ch, CURLOPT_HEADER, false);
    
    // 抓取URL并把它传递给浏览器
    curl_exec($ch);
    
    //关闭cURL资源,并且释放系统资源
    curl_close($ch);
    ?>
    

     2、head信息的设置

    $header [] = 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
    $header [] = 'Accept-Encoding:gzip, deflate';
    $header [] = 'Accept-Language:en';
    $header [] = 'Cache-Control:max-age=0';
    $header [] = 'Connection:keep-alive';
    $header [] = 'Cookie: PHPSESSID=jkuflj1jgoo7vvh5fcae3md262; qtrans_cookie_test=1';
    $header [] = 'DNT:1';
    $header [] = 'CLIENT-IP:58.68.44.61';
    $header [] = 'X-FORWARDED-FOR:58.68.44.61';
    //$header [] = 'Host:www.baidu.com';
    $header [] = 'If-Modified-Since:Tue, 08 Sep 2015 03:07:27 GMT';
    $header [] = 'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0';
    

      3、setopt信息的设置

    $cookie = tempnam('/data/www/tmp/','seek');
    curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_AUTOREFERER,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    if($type == 'POST' && !empty($data)){ //POST && is not empty $data
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    }
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($ch, CURLOPT_TIMEOUT,120);
    $content = curl_exec($ch);                                                                                                        
    curl_close($ch);
    

      4、文件保存

    $tp = @fopen($path, 'a');
    fwrite($tp, $content);
    fclose($tp);
    

     5、登录ftp

    <?php
        $username = 'username';
        $password = 'password';
        $url = 'example.com';
        $ftp_server = "ftp://" . $username . ":" . $password . "@" . $url;
       
        echo "Starting CURL.
    ";
        $ch = curl_init();
        echo "Set CURL URL.
    ";
       
        //curl FTP
        curl_setopt($ch, CURLOPT_URL, $ftp_server);
       
        //For Debugging
        //curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
       
        //SSL Settings
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
       
        //List FTP files and directories
        curl_setopt($ch, CURLOPT_FTPLISTONLY, TRUE);
       
        //Output to curl_exec
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
        echo "Executing CURL.
    ";
        $output = curl_exec($ch);
        curl_close($ch);
        echo "Closing CURL.
    ";
        echo $output . "
    ";
    
       $files = explode("
    ", $output);
       print_r($files);
    ?>
    

     5、使用代理

    <?php 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.baidu.com');
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    curl_setopt($ch, CURLOPT_PROXY, 'proxy.lxvoip.com:1080');
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');
    $data = curl_exec();
    curl_close($ch);
    ?>
    

     一些参数说明

    1、启用调试 

    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    

    2、ssl设置

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    
    or
    
    curl_setopt($ch, CURLOPT_VERBOSE, '1');
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, '2');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, '1');
    curl_setopt($ch, CURLOPT_CAINFO,  getcwd().'/cert/ca.crt');
    curl_setopt($ch, CURLOPT_SSLCERT, getcwd().'/cert/mycert.pem');
    curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'password'); 
    

    3、session

    $cookie = tempnam('/data/www/tmp/','seek');
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie); 
    

     4、参数解释

    CURLOPT_AUTOREFERER  当根据Location:重定向时,自动设置header中的Referer:信息
    CURLOPT_FOLLOWLOCATION 启用时会将服务器服务器返回的"Location: "放在header中递归的返回给服务器,使用CURLOPT_MAXREDIRS可以限定递归返回的数量
    CURLOPT_HEADER 启用时会将头文件的信息作为数据流输出(保存图片的时候要关闭)
    CURLOPT_NOBODY 启用时将不对HTML中的BODY部分进行输出
    CURLOPT_POST 启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样
    CURLOPT_RETURNTRANSFER 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
    CURLOPT_TIMEOUT  设置cURL允许执行的最长秒数
    CURLOPT_COOKIE 设定HTTP请求中"Cookie: "部分的内容。多个cookie用分号分隔,分号后带一个空格(例如, "fruit=apple; colour=red")
    CURLOPT_COOKIEFILE 包含cookie数据的文件名,cookie文件的格式可以是Netscape格式,或者只是纯HTTP头部信息存入文件
    CURLOPT_COOKIEJAR 连接结束后保存cookie信息的文件
    CURLOPT_ENCODING HTTP 请求头中"Accept-Encoding: "的值。支持的编码有"identity","deflate"和"gzip"。如果为空字符串"",请求头会发送所有支持的编码类型
    CURLOPT_POSTFIELDS  全部数据使用HTTP协议中的"POST"操作来发送
    CURLOPT_HTTPHEADER 一个用来设置HTTP头字段的数组。使用如下的形式的数组进行设置: array('Content-type: text/plain', 'Content-length: 100')
    

    参考地址 : http://php.net/manual/zh/function.curl-setopt.php

  • 相关阅读:
    导航
    占位
    django(一)
    进程与线程
    网络编程
    反射 单例模式
    面向对象及命名空间
    logging,包
    模块(二)os hashlib
    装饰器&递归
  • 原文地址:https://www.cnblogs.com/chenpingzhao/p/4796393.html
Copyright © 2020-2023  润新知