Php 从对通过curl发出的请求的响应中获取位置标头时出现问题
我已经通过使用curl发布数据登录了一个网站。我现在想显示用户登录后通常会看到的页面,但我无法显示,因为url总是会更改 abcd 其中,abcd是一个看似随机的数字 我一直在尝试获取响应头,但它一直在给我刚才发送的请求头Php 从对通过curl发出的请求的响应中获取位置标头时出现问题,php,curl,http-headers,Php,Curl,Http Headers,我已经通过使用curl发布数据登录了一个网站。我现在想显示用户登录后通常会看到的页面,但我无法显示,因为url总是会更改 abcd 其中,abcd是一个看似随机的数字 我一直在尝试获取响应头,但它一直在给我刚才发送的请求头 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://example.com/niceday/dirlogin.php'); //login URL curl_setopt ($ch, CURLOPT_POST, 1
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/niceday/dirlogin.php'); //login URL
curl_setopt ($ch, CURLOPT_POST, 1);
$postData = 'userName=scott&password=abc123& etc...';
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$store = curl_exec($ch);
$info = curl_getinfo($ch);
print_r($info);//wrong headers are printed out
显示了curl_exec($ch)的头,但如何获取响应头?
我不确定它是否相关,但输入登录凭据的表单使用javascript您可以获得以下响应标题行:
$headers=curl\u getinfo($ch)代码>但我看不出它将如何帮助您解决问题,然后您可以使用http\u parse\u header()
或explode(“\n”,$headers”)代码>
更新:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.mozilla.org/');
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true); // HTTP request is 'HEAD'
$headers=curl_exec($ch);
这将只返回标题。您可以通过以下方式获得响应标题行:$headers=curl\u getinfo($ch)代码>但我看不出它将如何帮助您解决问题,然后您可以使用http\u parse\u header()
或explode(“\n”,$headers”)代码>
更新:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.mozilla.org/');
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true); // HTTP request is 'HEAD'
$headers=curl_exec($ch);
这将只返回头。的文档说明:“获取服务器响应HTTP请求发送的所有头”。实际上应该是:“获取服务器为响应导致当前脚本执行的HTTP请求而发送的所有标头”。的文档说明:“获取服务器为响应HTTP请求而发送的所有标头”。实际上应该是:“获取服务器为响应导致当前脚本执行的HTTP请求而发送的所有标头”。尝试以下操作:
<?php
function my_get_headers($url ) {
$url_info=parse_url($url);
if (isset($url_info['scheme']) && $url_info['scheme'] == 'https') {
$port = 443;
@$fp=fsockopen('ssl://'.$url_info['host'], $port, $errno, $errstr, 10);
} else {
$port = isset($url_info['port']) ? $url_info['port'] : 80;
@$fp=fsockopen($url_info['host'], $port, $errno, $errstr, 10);
}
if($fp) {
stream_set_timeout($fp, 10);
$head = "HEAD ".@$url_info['path']."?".@$url_info['query'];
$head .= " HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n";
fputs($fp, $head);
while(!feof($fp)) {
if($header=trim(fgets($fp, 1024))) {
$sc_pos = strpos( $header, ':' );
if( $sc_pos === false ) {
$headers['status'] = $header;
} else {
$label = substr( $header, 0, $sc_pos );
$value = substr( $header, $sc_pos+1 );
$headers[strtolower($label)] = trim($value);
}
}
}
return $headers;
}
else {
return false;
}
}
print_r( my_get_headers("http://www.mozilla.org"));
?>
函数我的\u get\u头从中被盗请尝试以下操作:
<?php
function my_get_headers($url ) {
$url_info=parse_url($url);
if (isset($url_info['scheme']) && $url_info['scheme'] == 'https') {
$port = 443;
@$fp=fsockopen('ssl://'.$url_info['host'], $port, $errno, $errstr, 10);
} else {
$port = isset($url_info['port']) ? $url_info['port'] : 80;
@$fp=fsockopen($url_info['host'], $port, $errno, $errstr, 10);
}
if($fp) {
stream_set_timeout($fp, 10);
$head = "HEAD ".@$url_info['path']."?".@$url_info['query'];
$head .= " HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n";
fputs($fp, $head);
while(!feof($fp)) {
if($header=trim(fgets($fp, 1024))) {
$sc_pos = strpos( $header, ':' );
if( $sc_pos === false ) {
$headers['status'] = $header;
} else {
$label = substr( $header, 0, $sc_pos );
$value = substr( $header, $sc_pos+1 );
$headers[strtolower($label)] = trim($value);
}
}
}
return $headers;
}
else {
return false;
}
}
print_r( my_get_headers("http://www.mozilla.org"));
?>
函数我的“获取”标题是从偷来的,但你真的试过搜索谷歌吗?比如得到响应头卷曲?我不明白这不是懒惰,因为在这里发帖比搜索谷歌更累(我不是责备你或其他什么,只是我不明白),但你真的试过搜索谷歌吗?比如得到响应头卷曲?我不明白这不是懒惰,因为在这里发帖比查看谷歌更累(我不是责备你或其他什么,只是我不明白),但这些是我刚才提出的请求的标题,而不是服务器的响应。例如,$headers缺少我需要的位置标头。仍然存在相同的问题。我编辑了我的问题,以包含所有相关的代码。我尝试了它,它的工作。。。头包含在我的代码中的$headers中,只需尝试使用我的代码,然后使用YoursB进行调整,但这些是我刚才发出的请求的头,而不是服务器的响应。例如,$headers缺少我需要的位置标头。仍然存在相同的问题。我编辑了我的问题,以包含所有相关的代码。我尝试了它,它的工作。。。在我的代码中,头包含在$headers中,只需尝试使用我的代码,然后使用您的头进行调整即可。谢谢,现在可以使用了。get_headers()和curl_getinfo()返回了我没有预料到的结果,这把事情搞砸了。谢谢,它现在可以工作了。get_headers()和curl_getinfo()返回了我没有预料到的结果,这把事情搞砸了。