原创, 服务器

PHP远程获取网页内容类和调用方法

下面的方法方便以后再远程获取网页内容时候使用。

requestRemote.php


< ?php
/*
 * get Info Mode :
 *          1: body;
 *          0: header;
 *          2: validate server
 * myRequestRemote::requestRemote($url, $mode)
 *
*/

class myRequestRemote{
    static  $timeout = 3;
    static function requestRemote($url,$mode=1){
        /*
         * get Info Mode :
         *          1: body;
         *          0: header;
        */
        if(!empty($url)){
            if(function_exists("curl_init")){
                return self::curlRequestRemote($url,$mode);
            } else if(ini_get("allow_url_fopen")) {
                if($mode == "1") {
                    return self::filegetContentsRequestRemote($url);
                } else {
                    return self::fsockopenRequestRemote($url);
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    static function curlRequestRemote($url,$mode) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, self::$timeout);
        //curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
        //curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if($mode == "0") {
            curl_setopt($ch, CURLOPT_HEADER, true);
        }

        $r = curl_exec($ch);
        curl_close($ch);
        if($mode == "0") {
            return substr($r,0,17);
        }
        return $r;
    }

    static function filegetContentsRequestRemote($url) {
        $context = stream_context_create(array(
                'http' => array(
                        'timeout' => self::$timeout      // Timeout in seconds
                )
        ));
        return @file_get_contents($url,0,$context);
    }

    static function fsockopenRequestRemote($url) {
        $url = @parse_url($url);
        if($fp = @fsockopen($url['host'],empty($url['port'])?80:$url['port'],$errorno,$error,self::$timeout)) {

            fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n");
            fputs($fp,"Host:$url[host]\r\n\r\n");
            while(!feof($fp)) {
                $tmp = fgets($fp);
                if(trim($tmp) == '') {
                    break;
                } else if(preg_match_all('|HTTP(.*)|U',$tmp,$arr)) {
                    return $tmp;
                }
            }
            return false;
        } else {
            return false;
        }
    }
}
?>

调用上面的方法代码如下:


require_once("requestRemote.php");
//获取头部信息
$headers = myRequestRemote::requestRemote($url,0);
if(substr($headers, 9, 3) == '200'){
  //获取内容成功
}

//获取网页内容
$data = myRequestRemote::requestRemote($url,1);
//处理网页内容$data

(906)

Related Post