例1,用file_get_contents 以get方式获取内容。
<?php
$url='http://www.baidu.com/';//访问地址
$html = file_get_contents($url);//读取网页
print_r($html);//打印信息
?>
例2,用fopen打开url, 以get方式获取内容。
<?
$url='http://www.baidu.com/';
$fp = fopen($url, 'r');//打开网页
print_r(stream_get_meta_data($fp)); //打印出访问的网页表头信息
$result = '';
while(!feof($fp)) {//文件指针不为空的话读取文本
$result .= fgets($fp, 1024);
}
echo "url body: $result";//打印出来
fclose($fp);//关闭网页
?>
例3,用file_get_contents函数,以post方式获取url。
<?php
//发包数据
$data = array ('key' => '54e48ed53ee9c9266432350ad54429cf',
'phone'=>'13888888888'
);
//转换成发包请求字符串
$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://apis.juhe.cn/mobile/get', false, $context);
echo $html;
?>