PHPä¸æå¼URLå°åçå ç§æ¹æ³æ»ç»ï¼è¿éçå½æ°ä¸»è¦ç¨äºå°å·ééçå½æ°ã
1: ç¨file_get_contents
以getæ¹å¼è·åå
容
å¤å¶ä»£ç 代ç å¦ä¸:
<?php
$url='
http://www.baidu.com/'; $html = file_get_contents($url);
//print_r($http_response_header);
ec($html);
printhr();
printarr($http_response_header);
printhr();
?>
示ä¾ä»£ç 2: ç¨fopenæå¼url,
以getæ¹å¼è·åå
容
å¤å¶ä»£ç 代ç å¦ä¸:
<?
$fp = fopen($url, 'r');
printarr(stream_get_meta_data($fp));
printhr();
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
echo "url body: $result";
printhr();
fclose($fp);
?>
示ä¾ä»£ç 3ï¼ç¨file_get_contentså½æ°,以postæ¹å¼è·åurl
å¤å¶ä»£ç 代ç å¦ä¸:
<?php
$data = array ('foo' =>
'bar');
$data = http_build_query($data);
$opts = array (
'http'
=> array (
'method' => 'POST',
'header'=> "Content-type:
application/x-www-form-urlencoded" .
"Content-Length: " . strlen($data) .
"",
'content' => $data
),
);
$context =
stream_context_create($opts);
$html =
file_get_contents('
http://localhost/e/admin/test.html', false, $context);
echo $html;
?>
示ä¾ä»£ç 4ï¼ç¨fsockopenå½æ°æå¼urlï¼ä»¥getæ¹å¼è·åå®æ´çæ°æ®ï¼å
æ¬headeråbody
å¤å¶ä»£ç 代ç å¦ä¸:
<?
function get_url
($url,$cookie=false) {
$url = parse_url($url);
$query =
$url[path]."?".$url[query];
ec("Query:".$query);
$fp = fsockopen(
$url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
if (!$fp) {
return false;
} else {
$request = "GET $query HTTP/1.1";
$request .= "Host: $url[host]";
$request .= "Connection: Close";
if($cookie) $request.="Cookie: $cookie\n";
$request.="";
fwrite($fp,$request);
while(!@feof($fp)) {
$result .= @fgets($fp,
1024);
}
fclose($fp);
return $result;
}
}
//è·åurlçhtmlé¨åï¼å»æheader
function GetUrlHTML($url,$cookie=false) {
$rowdata = get_url($url,$cookie);
if($rowdata)
{
$body=
stristr($rowdata,"");
$body=substr($body,4,strlen($body));
return $body;
}
return false;
}
?>