=== 方法一 ===
function grabContentByUrl($url){
$curl = curl_init($url);
$header_string_1 = "Content-Type: application/html";
$header_string_2 = "Accept: application/html";
curl_setopt($curl, CURLOPT_HTTPHEADER, array($header_string_1, $header_string_2));
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if (curl_errno($curl)) {
return false;
} else {
curl_close($curl);
}
return $response;
}if($pagecontent = grabContentByUrl('http://www.google.com')){
echo $pagecontent;
}
function grabContentByUrl($url){
$curl = curl_init($url);
$header_string_1 = "Content-Type: application/html";
$header_string_2 = "Accept: application/html";
curl_setopt($curl, CURLOPT_HTTPHEADER, array($header_string_1, $header_string_2));
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if (curl_errno($curl)) {
return false;
} else {
curl_close($curl);
}
return $response;
}if($pagecontent = grabContentByUrl('http://www.google.com')){
echo $pagecontent;
}
function grabContentByUrl($url){
return file_get_contents($url);
}echo grabContentByUrl('http://www.google.com');
function grabContentByUrl($url){
return file_get_contents($url);
}echo grabContentByUrl('http://www.google.com');
<?php
$messageurl="http://www.google.com";
if ($messageurl."" <> "") {
echo getURLContent($messageurl);
}
function getURLContent($sURL)
{
$pURL=parse_url($sURL); $pScheme = $pURL["scheme"];
$pServer = $pURL["host"];
$pPort = ($pURL["port"]=="")?80:$pURL["port"];
$pPath = $pURL["path"];
$pPath .= ($pURL["query"]=="")?"":"?".$pURL["query"];
$fp = fsockopen($pServer,80,$errNo,$errStr,30); if (!$fp) {
return ""; } else {
$out = "GET ". '/'.$pPath." HTTP/1.0\r\n"; // $out = "GET ".$pServer .$pPath." HTTP/1.0\r\n";
$out .= "Accept: */*\r\n";
$out .= "Host: ".$pServer."\r\n";
$out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out);
$retContent="";
while (!feof($fp)) {
$retContent.=fgets($fp, 4096);
}
fclose($fp);
return $retContent;
}
} ?> 查了一下http协议出错在于请求资源或url不对要么给url,要么指定/根域
$out = "GET ". '/'.$pPath." HTTP/1.0\r\n"; //或$out = "GET ".$pServer .$pPath." HTTP/1.0\r\n";
$out .= "Accept: */*\r\n"; //