2010
12.16
12.16
<?php
$postdata = array('name'=>'niutian365','pass'=>'123456***') ;/*要访问的页面上的form内的input名称参数或者get参数*/
$post_string = http_build_query($postdata);
$opts = array(
'http'=>array(
'protocol_version'=>'1.1',/*http协议版本(若不指定php5.2系默认为http1.0)*/
'method'=>"POST",/*获取方式这里可改成GET*/
'timeout' => 5 ,/*超时时间*/
'header'=>
"User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8\r\n".
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".
"Accept-language: zh-cn,zh;q=0.5\r\n" .
"Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n".
"Content-length: ".strlen($post_string),/*模拟的header头*/
'content'=> $post_string /*访问时传递的数据内容*/
)
);
/*创建并返回一个文本数据流并应用各种选项,
可用于fopen(),file_get_contents()等过程的超时设置,
代理服务器,请求方式,头信息设置的特殊过程
*/
$context = stream_context_create($opts);
$content = file_get_contents('http://www.niutian365.com/index.php',false,$context );
?>
其他方式(来自网络未验证):
<?php
/**
* Socket版本
* 使用方法:
* $post_string = "app=socket&version=beta";
* request_by_socket('facebook.cn','/restServer.php',$post_string);
*/
function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30){
$socket = fsockopen($remote_server,$port,$errno,$errstr,$timeout);
if (!$socket) die("$errstr($errno)");
fwrite($socket,"POST $remote_path HTTP/1.0\r\n");
//fwrite($socket,"User-Agent: Socket Example\r\n");
fwrite($socket,"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2\r\n");
fwrite($socket,"HOST: $remote_server\r\n");
fwrite($socket,"Content-type: application/x-www-form-urlencoded\r\n");
fwrite($socket,"Content-length: ".strlen($post_string) ."\r\n");
fwrite($socket,"Accept:*/*\r\n");
fwrite($socket,"\r\n");
fwrite($socket,$post_string."\r\n");
fwrite($socket,"\r\n");
$header = "";//去除头部
while ($str = trim(fgets($socket,4096))) {
$header.=$str;
}
$data = "";
while (!feof($socket)) {
$data .= fgets($socket,4096);
}
return $data;
}
/**
* Curl版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_curl('http://facebook.cn/restServer.php',$post_string);
*/
function request_by_curl($remote_server,$post_string){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$remote_server);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2');
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
暂无回复
添加回复