啥也不说,上代码:
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) // CRC at end of file
$headerlen += 2;
$unpacked = @gzinflate ( substr ( $data, $headerlen ) );
if ($unpacked === FALSE)
$unpacked = $data;
return $unpacked;
}
}
$fp = fsockopen ( “www.amazon.cn”, 80, $errno, $errstr, 30 );
if (! $fp) {
echo “$errstr ($errno)<br />\n”;
} else {
$out = “GET /mn/detailApp?asin=B003L21OCC HTTP/1.1\r\n”;
$out .= “Host: www.amazon.cn\r\n”;
$out .= “User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: zh-cn,zh;q=0.5\r\nAccept-Encoding: gzip;deflate\r\nAccept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 115\r\nConnection: keep-alive\r\nReferer: http://www.ncbi.nlm.nih.gov/sites/entrez\r\n”;
$out .= “\r\n\r\n”;
fwrite ( $fp, $out );
$Data = ”;
while ( $a = fgets ( $fp ) ) {
$Data .= $a;
}
fclose ( $fp );
}
$pos = strpos ( $Data, “\r\n\r\n” );
if ($pos === false) {
$jj .= $Data;
} else {
$HeadInfoStr = substr ( $Data, 0, $pos );
$HeadInfo = explode ( “\r\n”, $HeadInfoStr );
$Head = array ();
$State = 0;
if (count ( $HeadInfo ) > 0) {
$Arr = explode ( ” “, $HeadInfo [0] );
$State = intval ( $Arr [1] );
$Head ['State'] = $State;
array_shift ( $HeadInfo );
}
foreach ( $HeadInfo as $v ) {
$posm = strpos ( $v, ‘:’ );
if ($posm > 0) {
$HeadKey = strtolower ( trim ( substr ( $v, 0, $posm ) ) );
$HeadStrArr = explode ( ‘;’, substr ( $v, $posm + 1, strlen ( $v ) – $posm ) );
$Head [$HeadKey] = ltrim ( $HeadStrArr [0] );
if (count ( $HeadStrArr ) > 1) {
$posm = strpos ( $HeadStrArr [1], ‘=’ );
if ($posm > 0) {
$Head [strtolower ( trim ( substr ( $HeadStrArr [1], 0, $posm ) ) )] = substr ( $HeadStrArr [1], $posm + 1, strlen ( $HeadStrArr [1] ) – $posm );
}
}
}
}
$jj = substr ( $Data, $pos, strlen ( $Data ) – $pos );
$j = $Head;
}
print_r ( $j ); //header info.
echo $result = gzdecode ( transfer_encoding_chunked_decode (ltrim($jj) ) ); //content
function transfer_encoding_chunked_decode($in) {
$out = ”;
while ( $in != ” ) {
$lf_pos = strpos ( $in, “\012″ );
if ($lf_pos === false) {
$out .= $in;
break;
}
$chunk_hex = trim ( substr ( $in, 0, $lf_pos ) );
$sc_pos = strpos ( $chunk_hex, ‘;’ );
if ($sc_pos !== false)
$chunk_hex = substr ( $chunk_hex, 0, $sc_pos );
if ($chunk_hex == ”) {
$out .= substr ( $in, 0, $lf_pos );
$in = substr ( $in, $lf_pos + 1 );
continue;
}
$chunk_len = hexdec ( $chunk_hex );
if ($chunk_len) {
$out .= substr ( $in, $lf_pos + 1, $chunk_len );
$in = substr ( $in, $lf_pos + 2 + $chunk_len );
} else {
$in = ”;
}
}
return $out;
}