最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php實現(xiàn)httpRequest的方法

 更新時間:2015年03月13日 10:36:52   作者:令狐不聰  
這篇文章主要介紹了php實現(xiàn)httpRequest的方法,涉及php操作http的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了php實現(xiàn)httpRequest的方法。分享給大家供大家參考。具體如下:

想從學校圖書館的網(wǎng)站上抓取數(shù)據(jù)處理之后在返回給瀏覽器,試了不少方法。首先試了http_request(),但是這個學院pecl_http支持,后來又試了網(wǎng)上流傳甚廣的class HttpRequest,可能是我不會使用,也失敗了。后來看到了函數(shù)httpRequest($url, $post='', $method='GET', $limit=0, $returnHeader=FALSE, $cookie='', $bysocket=FALSE, $ip='', $timeout=15, $block=TRUE),用它成功了,因此貼出來分享一下。函數(shù)代碼如下:

復制代碼 代碼如下:
<?php 
    /**
    * Respose A Http Request
    *
    * @param string $url
    * @param array $post
    * @param string $method
    * @param bool $returnHeader
    * @param string $cookie
    * @param bool $bysocket
    * @param string $ip
    * @param integer $timeout
    * @param bool $block
    * @return string Response
    */ 
    function httpRequest($url,$post='',$method='GET',$limit=0,$returnHeader=FALSE,$cookie='',$bysocket=FALSE,$ip='',$timeout=15,$block=TRUE) { 
       $return = ''; 
       $matches = parse_url($url); 
       !isset($matches['host']) && $matches['host'] = ''; 
       !isset($matches['path']) && $matches['path'] = ''; 
       !isset($matches['query']) && $matches['query'] = ''; 
       !isset($matches['port']) && $matches['port'] = ''; 
       $host = $matches['host']; 
       $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/'; 
       $port = !empty($matches['port']) ? $matches['port'] : 80; 
       if(strtolower($method) == 'post') { 
           $post = (is_array($post) and !empty($post)) ? http_build_query($post) : $post; 
           $out = "POST $path HTTP/1.0\r\n"; 
           $out .= "Accept: */*\r\n"; 
           //$out .= "Referer: $boardurl\r\n"; 
           $out .= "Accept-Language: zh-cn\r\n"; 
           $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
           $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"; 
           $out .= "Host: $host\r\n"; 
           $out .= 'Content-Length: '.strlen($post)."\r\n"; 
           $out .= "Connection: Close\r\n"; 
           $out .= "Cache-Control: no-cache\r\n"; 
           $out .= "Cookie: $cookie\r\n\r\n"; 
           $out .= $post; 
       } else { 
           $out = "GET $path HTTP/1.0\r\n"; 
           $out .= "Accept: */*\r\n"; 
           //$out .= "Referer: $boardurl\r\n"; 
           $out .= "Accept-Language: zh-cn\r\n"; 
           $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"; 
           $out .= "Host: $host\r\n"; 
           $out .= "Connection: Close\r\n"; 
           $out .= "Cookie: $cookie\r\n\r\n"; 
       } 
       $fp = fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout); 
       if(!$fp) return ''; else { 
           $header = $content = ''; 
           stream_set_blocking($fp, $block); 
           stream_set_timeout($fp, $timeout); 
           fwrite($fp, $out); 
           $status = stream_get_meta_data($fp); 
           if(!$status['timed_out']) {//未超時 
               while (!feof($fp)) { 
                   $header .= $h = fgets($fp); 
                   if($h && ($h == "\r\n" ||  $h == "\n")) break; 
               } 
 
               $stop = false; 
               while(!feof($fp) && !$stop) { 
                   $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit)); 
                   $content .= $data; 
                   if($limit) { 
                       $limit -= strlen($data); 
                       $stop = $limit <= 0; 
                   } 
               } 
           } 
        fclose($fp); 
           return $returnHeader ? array($header,$content) : $content; 
       } 
    } 
?>

調(diào)用也很簡單的。簡單的例子:

復制代碼 代碼如下:
echo httpRequest('http://www.baidu.com');

希望本文所述對大家的php程序設計有所幫助。

相關文章

  • php淺析反序列化結(jié)構(gòu)

    php淺析反序列化結(jié)構(gòu)

    序列化其實就是將數(shù)據(jù)轉(zhuǎn)化成一種可逆的數(shù)據(jù)結(jié)構(gòu),自然,逆向的過程就叫做反序列化。php將數(shù)據(jù)序列化和反序列化會用到兩個函數(shù):serialize?將對象格式化成有序的字符串、unserialize?將字符串還原成原來的對象
    2022-07-07
  • PHP中集成PayPal標準支付的實現(xiàn)方法分享

    PHP中集成PayPal標準支付的實現(xiàn)方法分享

    前兩天一個客戶需要在網(wǎng)站上集成PayPal支付功能,查了一下資料,簡單記錄如下
    2012-02-02
  • PHP微信支付結(jié)果通知與回調(diào)策略分析

    PHP微信支付結(jié)果通知與回調(diào)策略分析

    這篇文章主要介紹了PHP微信支付結(jié)果通知與回調(diào)策略,結(jié)合實例形式分析了php微信支付結(jié)果的回調(diào)處理相關操作技巧,需要的朋友可以參考下
    2019-01-01
  • PHP面相對象中的重載與重寫

    PHP面相對象中的重載與重寫

    本文主要介紹了PHP面相對象中的重載與重寫。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • THINKPHP支持YAML配置文件的設置方法

    THINKPHP支持YAML配置文件的設置方法

    這篇文章主要介紹了THINKPHP支持YAML配置文件的設置方法,本文講解了為什么要用 yaml以及在THINKPHP中的配置方法,需要的朋友可以參考下
    2015-03-03
  • PHP基于SimpleXML生成和解析xml的方法示例

    PHP基于SimpleXML生成和解析xml的方法示例

    這篇文章主要介紹了PHP基于SimpleXML生成和解析xml的方法,結(jié)合完整實例形式分析了php使用SimpleXML生成及解析xml格式數(shù)據(jù)的具體操作技巧,需要的朋友可以參考下
    2017-07-07
  • PHP實現(xiàn)猜數(shù)游戲

    PHP實現(xiàn)猜數(shù)游戲

    這篇文章主要為大家詳細介紹了PHP實現(xiàn)猜數(shù)游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • php pdo oracle中文亂碼的快速解決方法

    php pdo oracle中文亂碼的快速解決方法

    下面小編就為大家?guī)硪黄猵hp pdo oracle中文亂碼的快速解決方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考,一起跟隨小編過來看看吧
    2016-05-05
  • php+mysql實現(xiàn)用戶注冊登陸的方法

    php+mysql實現(xiàn)用戶注冊登陸的方法

    這篇文章主要介紹了php+mysql實現(xiàn)用戶注冊登陸的方法,可實現(xiàn)簡單的用戶注冊登錄的功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-01-01
  • PHP數(shù)組的交集array_intersect(),array_intersect_assoc(),array_inter_key()函數(shù)的小問題

    PHP數(shù)組的交集array_intersect(),array_intersect_assoc(),array_inte

    求兩個數(shù)組的交集問題可以使用array_intersect(),array_inersect_assoc,array_intersect_key來實現(xiàn),其中array_intersect()函數(shù)是求兩個數(shù)的交集
    2011-05-05

最新評論

龙江县| 石屏县| 嘉荫县| 密云县| 阿克陶县| 曲松县| 武陟县| 开鲁县| 铜梁县| 横峰县| 平遥县| 翼城县| 沈阳市| 微山县| 阿克苏市| 股票| 吉木乃县| 永济市| 南溪县| 安阳县| 鹤山市| 喀喇| 航空| 蚌埠市| 琼海市| 云林县| 阆中市| 湟中县| 瓦房店市| 安吉县| 游戏| 洛隆县| 确山县| 广平县| 蛟河市| 阿勒泰市| 丽江市| 禄丰县| 柘荣县| 隆安县| 安泽县|