如何获取http请求后,返回的头部信息 我构建了http(post)请求,发出后,我想获取http反应回来的头部信息 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 $username=$_POST['username']; $password=$_POST['password']; $host="www.teamsun.com.cn:88"; $service_uri="/login.msc"; $query_string="user=".$username."&password=".$password."&mode=submit"; $http_request="POST ". $service_uri ." HTTP/1.1\r\n"; $http_request.="Host:$host\r\n"; $http_request.="Content-Type: application/x-www-form-urlencoded\r\n"; $http_request.="Accept-Encoding: gzip, deflate\r\n"; $http_request.="Content-Length:".strlen($query_string)."\r\n"; $http_request.="Connection: Keep-Alive\r\n"; $http_request.="Cache-Control: no-cache\r\n\r\n"; $http_request.=$query_string."\r\n\r\n"; $fs = fsockopen($host,88); if (!is_resource($fs)){ echo "trackback_send: Couldn't connect to $host,Port 88"; return false; } fwrite($fs, $http_request); while(!feof($fs)) { $response.=fread($fs,4096); } fclose($fs); $headers = getallheaders(); while (list($header, $value) = each($headers)) { echo "$header: $value<br>\n"; }打印的为空值 fwrite 后. 跟着用 用 fgets 读取数据得到返回结果. while (!feof($fp)) { $line.=fgets($fp, 512); 楼上的这没关系吧,fgets碰都\r\n就结束了 $host="www.teamsun.com.cn:88";==============================你确定host是这样的?怎么还带端口号? 这个没关系,是不是由于getallheaders (),他是获取每个请求的http头部信息,那如何取得返回来的 肯定是 getallheader 这个的问题.有好好的 curl 为什么不用? ...........echo $headers;//测试一下有无头信息输出?有,则把函数getallheaders贴出来看看。$headers = getallheaders();...... sorry,应该是==============echo $response//测试一下有无头信息输出?有,则把函数getallheaders贴出来看看。 他是php内部函数呀,echo $response打印的是请求的头部信息 哪位高手可以指点下呀,我是个php编程新手 ^_^,还真没用过这函数。那你用正则抓$response里的内容不得了? getallheaders 不是这样用的..... $response打印也为空值==============================================这个.............你运行你的程序没有错误提示吗? 那你就得调试一下到底哪里出了问题了========================================$fs = fsockopen($host,88) or die('无法连接'); getallheaders是用来获取用户请求头数据的...用curl多简单啊 为什么要自己写socket... PHP使用MSSQL2005数据库的效率高吗?跟MYSQL比 help! 简单的PHP问题。 PHP如何实现URL重写,谢谢 json数据如何解析? mysql_free_result和mysql_close哪个好? 删除文本记录问题 请教一个问题 www.51.net上的session问题,大家帮帮我啊 慕课网PHP商城 教程实际不显示验证码问题 模板volist遍历 关于正则问题 请问下面这段代码,怎么24小时执行一次
$password=$_POST['password'];
$host="www.teamsun.com.cn:88";
$service_uri="/login.msc";
$query_string="user=".$username."&password=".$password."&mode=submit";
$http_request="POST ". $service_uri ." HTTP/1.1\r\n";
$http_request.="Host:$host\r\n";
$http_request.="Content-Type: application/x-www-form-urlencoded\r\n";
$http_request.="Accept-Encoding: gzip, deflate\r\n";
$http_request.="Content-Length:".strlen($query_string)."\r\n";
$http_request.="Connection: Keep-Alive\r\n";
$http_request.="Cache-Control: no-cache\r\n\r\n";
$http_request.=$query_string."\r\n\r\n";
$fs = fsockopen($host,88);
if (!is_resource($fs)){
echo "trackback_send: Couldn't connect to $host,Port 88";
return false;
}
fwrite($fs, $http_request);
while(!feof($fs))
{
$response.=fread($fs,4096);
}
fclose($fs);
$headers = getallheaders();
while (list($header, $value) = each($headers)) {
echo "$header: $value<br>\n";
}
打印的为空值
用 fgets 读取数据得到返回结果. while (!feof($fp))
{
$line.=fgets($fp, 512);
==============================
你确定host是这样的?怎么还带端口号?
有好好的 curl 为什么不用?
...
..
..
echo $headers;//测试一下有无头信息输出?有,则把函数getallheaders贴出来看看。
$headers = getallheaders();
..
.
..
.
==============
echo $response//测试一下有无头信息输出?有,则把函数getallheaders贴出来看看。
那你用正则抓$response里的内容不得了?
==============================================
这个.............你运行你的程序没有错误提示吗?
========================================
$fs = fsockopen($host,88) or die('无法连接');
用curl多简单啊 为什么要自己写socket...