我通过curl发布数据登录了一个网站。我现在想要显示用户登录后通常会看到的页面,但我不能,因为网址总是在变化。
http://some.example.com/niceday/foobar.php?TID= ABCD 其中 abcd 是一些看似随机的数字。
我一直在尝试获取响应标头,但它一直向我提供我刚刚发送的请求标头。
$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
答案 0 :(得分:2)
你可以获得响应标题:$headers = curl_getinfo($ch);
但是我看不出它如何帮助你解决问题,然后你可以使用http_parse_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);
这将只返回标题。
答案 1 :(得分:1)
试试这个:
<?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"));
?>
输出:
Array
(
[status] => HTTP/1.1 200 OK
[server] => Apache
[x-backend-server] => pm-web01
[content-type] => text/html; charset=UTF-8
[date] => Mon, 15 Aug 2011 14:26:16 GMT
[keep-alive] => timeout=20, max=999
[expires] => Mon, 15 Aug 2011 00:36:16 GMT
[connection] => close
[x-powered-by] => PHP/5.2.9
[x-cache-info] => not cacheable; response has already expired
)
功能 my_get_headers 被盗http://www.php.net/manual/en/function.get-headers.php#64073
答案 2 :(得分:0)
get_gheaders
的文档说:“获取服务器发送的所有标头以响应HTTP请求”。实际应该是:“获取服务器发送的所有标头,以响应导致当前脚本执行的HTTP请求”。