PHPµÄCURL·½·¨curl_setopt()º¯Êý°¸Àý½éÉÜ(ץȡÍøÒ³,POSTÊý¾Ý)

5年以前  |  阅读数:372 次  |  编程语言:PHP 

ͨ¹ýcurl_setopt()º¯Êý¿ÉÒÔ½±a¿i½ÝµÄץȡÍøÒ³(²É¼¯ºÜ½±a´oЦ)£¬curl_setopt ÊÇPHPµÄÒ»¸oÀ(C)Õ¹¿a

ʹÓÃÌo¼þ£ºÐeÒªÔÚphp.ini ÖÐÅaÖÿªÆo¡£(PHP 4 >= 4.0.2)
//È¡ÏuÏÂÃaeµÄ×¢ÊÍ

extension=php_curl.dll

ÔÚLinuxÏÂÃae£¬ÐeÒªÖØбaÒePHPÁË£¬±aÒeʱ£¬ÄaÐeÒª´o¿ª±aÒe²ÎÊý¡ª¡ªÔÚconfigureÃuÁiÉϼÓÉÏ¡°¨Cwith-curl¡± ²ÎÊý¡£

1¡¢ Ò»¸oץȡÍøÒ³µÄ¼oµ¥°¸Àý£º


    [php] view plain copy print?
    // ´´½¨Ò»¸oÐÂcURL×ÊÔ´ 
    $ch = curl_init();  
    // ÉeÖÃURLºÍÏaÓ¦µÄÑ¡Ïi 
    curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com/"); 
    curl_setopt($ch, CURLOPT_HEADER, false);  
    // ץȡURL²¢°ÑËu´«µÝ¸øa¯ÀÀÆ÷ 
    curl_exec($ch); 
    //¹Ø±ÕcURL×ÊÔ´£¬²¢ÇÒÊÍ*Åϵͳ×ÊÔ´ 
    curl_close($ch); 

2¡¢POSTÊý¾Ý°¸Àý£º


    [php] view plain copy print?
    // ´´½¨Ò»¸oÐÂcURL×ÊÔ´ 
    $ch = curl_init(); 
    $data = 'phone='. urlencode($phone); 
    // ÉeÖÃURLºÍÏaÓ¦µÄÑ¡Ïi 
    curl_setopt($ch, CURLOPT_URL, "http://www.post.com/"); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    // ץȡURL²¢°ÑËu´«µÝ¸øa¯ÀÀÆ÷ 
    curl_exec($ch); 
    //¹Ø±ÕcURL×ÊÔ´£¬²¢ÇÒÊÍ*Åϵͳ×ÊÔ´ 
    curl_close($ch); 

3¡¢¹ØÓÚSSLºÍCookie

¹ØÓÚSSLÒ²¾ÍÊÇHTTPSЭÒe£¬ÄaÖ»ÐeÒª°ÑCURLOPT_URLÁ¬½ÓÖеÄhttp://±a³Éhttps://¾Í¿ÉÒÔÁË¡£µ±È»£¬»¹ÓÐÒ»¸o²ÎÊý½ÐCURLOPT_SSL_VERIFYHOST¿ÉÒÔÉeÖÃΪÑeÖ¤Õ¾µa¡£

¹ØÓÚCookie£¬ÄaÐeÒªÁ˽aÏÂÃaeÈý¸o²ÎÊý£º

  • CURLOPT_COOKIE£¬ÔÚµ±ÃaeµÄ»a»°ÖÐÉeÖÃÒ»¸ocookie
  • CURLOPT_COOKIEJAR£¬µ±»a»°½aÊøµÄʱºo±£´aeÒ»¸oCookie
  • CURLOPT_COOKIEFILE£¬CookieµÄÎļþ¡£

*PS:ÐÂÀË΢²(C)µÇ½API²¿Ö½ØÈ¡(²¿ÖÎÒÔo¼ÓÁ˵a×¢ÊÍ£¬È«µ±²ÎÊý­ÒeÏ¡£¹þ¹þ) ÓÐÐËȤµÄ×Ô¼ºÑо¿£¬×Ô¼ºÅ²Îª¼ºÓ᣺ٺÙ**


    [php] view plain copy print?
    /** 
       * Make an HTTP request 
       * 
       * @return string API results 
       * @ignore 
       */ 
      function http($url, $method, $postfields = NULL, $headers = array()) { 
        $this->http_info = array(); 
        $ci = curl_init(); 
        /* Curl settings */ 
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//ÈÃcURL×Ô¼ºÅжÏʹÓÃÄĸo°ae±¾ 
        curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);//ÔÚHTTPÇeÇoÖаuº¬Ò»¸o"User-Agent: "Í*µÄ×Ö*u´®¡£ 
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);//ÔÚ*¢ÆðÁ¬½ÓÇ°µÈ´ýµÄʱ¼a£¬Èç¹uÉeÖÃΪ0£¬ÔoÎÞÏ޵ȴý 
        curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);//ÉeÖÃcURLÔÊÐiÖ´ÐеÄ×i³¤ÃeÊý 
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);//*µ»ØÔ­ÉuµÄ£¨Raw£(C)Êa³o 
        curl_setopt($ci, CURLOPT_ENCODING, "");//HTTPÇeÇoÍ*ÖÐ"Accept-Encoding: "µÄÖµ¡£Ö§³ÖµÄ±aÂeÓÐ"identity"£¬"deflate"ºÍ"gzip"¡£Èç¹uΪ¿Õ×Ö*u´®""£¬ÇeÇoÍ*»a*¢ËÍËuÓÐÖ§³ÖµÄ±aÂeÀaÐÍ¡£ 
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);//½uÓúocURL½«ÖÕÖ¹´Ó*þÎñ¶Ë½øÐÐÑeÖ¤ 
        curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));//µÚÒ»¸oÊÇcURLµÄ×ÊÔ´¾a±u£¬µÚ¶þ¸oÊÇÊa³oµÄheaderÊý¾Ý 
        curl_setopt($ci, CURLOPT_HEADER, FALSE);//ÆoÓÃʱ»a½«Í*ÎļþµÄÐÅÏ¢×÷ΪÊý¾ÝÁ÷Êa³o 
        switch ($method) { 
          case 'POST': 
            curl_setopt($ci, CURLOPT_POST, TRUE); 
            if (!empty($postfields)) { 
              curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); 
              $this->postdata = $postfields; 
            } 
            break; 
          case 'DELETE': 
            curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); 
            if (!empty($postfields)) { 
              $url = "{$url}?{$postfields}"; 
            } 
        } 
        if ( isset($this->access_token) && $this->access_token ) 
          $headers[] = "Authorization: OAuth2 ".$this->access_token; 
        $headers[] = "API-RemoteIP: " . $_SERVER['REMOTE_ADDR']; 
        curl_setopt($ci, CURLOPT_URL, $url ); 
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers ); 
        curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE ); 
        $response = curl_exec($ci); 
        $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); 
        $this->http_info = array_merge($this->http_info, curl_getinfo($ci)); 
        $this->url = $url; 
        if ($this->debug) { 
          echo "=====post data======\r\n"; 
          var_dump($postfields); 

          echo '=====info====='."\r\n"; 
          print_r( curl_getinfo($ci) ); 

          echo '=====$response====='."\r\n"; 
          print_r( $response ); 
        } 
        curl_close ($ci); 
        return $response; 
      } 

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu±¾ÎĵÄÄÚÈݶԴo¼ÒµÄѧϰ»oÕß¹¤×÷ÄÜ´øÀ´Ò»¶¨µÄ°iÖu£¬Í¬Ê±Ò²Ï£Íu¶a¶aÖ§³Ö½Å±¾Ö®¼Ò£¡

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例