一、用file_get_contents函数,以post方式获取url
$url= 'http://www.domain.com/test.php?id=123';
$data= array('foo'=> 'bar');
$data= http_build_query($data);
$opts= array(
'http'=> array(
'method'=> 'POST',
'header'=>"Content-type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content'=> $data
)
);
$ctx= stream_context_create($opts);
$html= @file_get_contents($url,'',$ctx);
二、用file_get_contents以get方式获取内容
$url='http://www.domain.com/?para=123';
$html= file_get_contents($url);
echo$html;
?>
三、用fopen打开url, 以get方式获取内容
$fp= fopen($url,'r');
$header= stream_get_meta_data($fp);//获取报头信息
while(!feof($fp)) {
$result.= fgets($fp, 1024);
}
echo"url header: {$header}
":
echo"url body: $result";
fclose($fp);
?>
四、用fopen打开url, 以post方式获取内容
$data= array('foo2'=> 'bar2','foo3'=>'bar3');
$data= http_build_query($data);
$opts= array(
'http'=> array(
'method'=> 'POST',
'header'=>"Content-type: application/x-www-form-
urlencoded\r\nCookie:cook1=c3;cook2=c4\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content'=> $data
)
);
$context= stream_context_create($opts);
$html= fopen('http://www.test.com/zzzz.php?id=i3&id2=i4','rb',false, $context);
$w=fread($html,1024);
echo$w;
?>
五、使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展
$ch= curl_init();
$timeout= 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.domain.com/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents= curl_exec($ch);
curl_close($ch);
echo$file_contents;
?>
此类方法一共有三种
第一种方法
$c = curl_init();
$url = 'www.badcatxt.com';
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($c);
curl_close($c);
$pos = strpos($data,'utf-8');
if($pos===false){$data = iconv("gbk","utf-8",$data);}
preg_match("/
echo $title[1];
?>
第二种方法:使用file()函数
$lines_array = file('http://www.badcatxt.com/');
$lines_string = implode('', $lines_array);
$pos = strpos($lines_string,'utf-8');
if($pos===false){$lines_string = iconv("gbk","utf-8",$lines_string);}
eregi("
echo $title[1];
?>
第三种方法:使用file_get_contents
$content=file_get_contents("http://www.badcatxt.com/");
$pos = strpos($content,'utf-8');
if($pos===false){$content = iconv("gbk","utf-8",$content);}
$postb=strpos($content,'
$poste=strpos($content,'
$length=$poste-$postb;
echo substr($content,$postb,$length);
?>
用正则表达式,是最快的,你看下面:
$url = 'http://www.baidu.com'; //这儿填页面地址
$info=file_get_contents($url);
preg_match('|
是这个 preg_match('/
用正则表达式,是最快的,你看下面:
$url
=
'http://www.baidu.com';
//这儿填页面地址
$info=file_get_contents($url);
preg_match('|