php调用远程url的六种方法
示例代码1: 用file_get_contents 以get方式获取内容
?
?
? 示例代码2: 用fopen打开url, 以get方式获取内容
$fp = fopen($url, 'r');
? ?
printarr(stream_get_meta_da
ta($fp));
printhr();
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
echo url body:? $result;
printhr();
fclose($fp);
?>
示例代码3:用file_get_contents函数,以post方式获取url
'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
),
);
$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\r\n;
$request .= host: $url[host]\r\n;
$request .= connection: close\r\n;
if($cookie) $request.=cookie: $cookie\n;
$request.=\r\n;
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,\r\n\r\n);
? $body=substr($body,4,strlen($body));
? return $body;
? }
? ?
? return false;
}
?>
示例代码5:用fsockopen函数打开url,以post方式获取完整的数据,包括header和body
function http_post($url,$data,$cookie, $referrer=) {
// parsing the given url
$url_info=parse_url($url);
// building referrer
if($referrer==) // if not given use this script as referrer
$referrer=111;
// making string from $data
foreach($data as $key=>$value)
$values[]=$key=.urlencode($value);
$data_string=implode(&,$values);
// find out which port is needed - if not given use standard (=80)
if(!isset($url_info[port]))
$url_info[port]=80;
// building post-request:
$request.=post .$url_info[path]. http/1.1\n;
$request.=host: .$url_info[host].\n;
$request.=referer: $referer\n;
$request.=content-type: application/x-www-form-urlencoded\n;
$request.=content-length: .strlen($data_string).\n;
$request.=connection: close\n;
$request.=cookie: $cookie\n;
$request.=\n;
$request.=$data_string.\n;
$fp = fsockopen($url_info[host],$url_info[port]);
fputs($fp, $request);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
fclose($fp);
return $result;
}
printhr();
?>
示例代码6:使用curl库,使用curl库之前,你可能需要查看一下php.ini,查看是否已经打开了curl扩展
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, curlopt_url, 'http://www.baidu.com/');
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_connecttimeout, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
关于curl库:
curl官方网站http://curl.haxx.se/
curl是使用url语法的传送文件工具,支持ftp、ftps、http htpps scp sftp tftp telnet dictfile和ldap。curl 支持ssl证书、http post、http put 、ftp上传,kerberos、基于htt格式的上传、代理、cookie、用户+口令证明、文件传送恢复、http代理通道和大量其他有用的技巧
function printarr(array $arr)
{
? echo
row field count: .count($arr).
;
? foreach($arr as $key=>$value)
? {
? ?
? echo $key=$value?
;
? }
}
?>