file_get_contents()函数的相关参数,支持SSL错误和header
$options = array( "ssl" => array( "verify_peer" => false, "verify_peer_name" => false, ), "http" => array( 'timeout '=> 2, 'method' => "GET", 'header' => "Accept: application/json\r\n" . "Accept-Encoding: gzip, deflate\r\n" . "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n" . // "Connection: keep-alive\r\n" . "Cookie: foo=bar\r\n" . "Host: www.jb51.net\r\n" . "Referer: http://www.baidu.com\r\n" . "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0\r\n" . "X-Requested-With: XMLHttpRequest", ), ); $f = file_get_contents("http://www.jb51.net", false, stream_context_create($options));
echo gzdecode($f); if(!function_exists('gzdecode')){ function gzdecode($data){ $flags = ord(substr($data, 3, 1)); $headerlen = 10; $extralen = 0; $filenamelen = 0; if($flags & 4){ $extralen = unpack('v', substr($data, 10, 2)); $extralen = $extralen[1]; $headerlen += 2 + $extralen; } if($flags & 8) // Filename $headerlen = strpos($data, chr(0), $headerlen) + 1; if($flags & 16) // Comment $headerlen = strpos($data, chr(0), $headerlen) + 1; if($flags & 2) // CRCatendoffile $headerlen += 2; $unpacked = @gzinflate(substr($data, $headerlen)); if($unpacked === FALSE) $unpacked = $data; return $unpacked; } }