简体   繁体   English

从通过curl发出的请求的响应中获取位置标头时遇到麻烦

[英]trouble getting the location header from a response to a request made through curl

I have logged into a website by posting data with curl. 我已经通过curl发布数据登录了一个网站。 I now want to display the page that a user would normally see after logging in but I can't because the url always changes. 现在,我想显示用户登录后通常会看到的页面,但是我不能显示,因为URL总是在变化。

http://some.example.com/niceday/foobar.php?TID= abcd where abcd is some seemingly random number. http://some.example.com/niceday/foobar.php?TID= abcd ,其中abcd是一些看似随机的数字。

I've been trying to get the response headers but it keeps giving me the request headers I just sent. 我一直在尝试获取响应标头,但它一直为我提供刚发送的请求标头。

$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

The headers for the curl_exec($ch) are shown but how do I get the response headers? 显示了curl_exec($ ch)的标题,但是如何获取响应标题? I'm not sure if it's relevant but the form where the login credentials are entered uses javascript 我不确定是否相关,但是输入登录凭据的表单使用的是javascript

YOu can get the response header line this : $headers = curl_getinfo($ch); 您可以这样获得响应标题行: $headers = curl_getinfo($ch); But i can't see how it will help you with your problem and then you can use http_parse_header() or explode("\\n", $headers); 但是我看不到它将如何帮助您解决问题,然后可以使用http_parse_header()explode("\\n", $headers);

update : 更新:

$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);

This will return only the headers. 这将仅返回标题。

Try this: 尝试这个:

<?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"));  

?>

OUTPUTS: 输出:

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
)

Function my_get_headers was stolen from http://www.php.net/manual/en/function.get-headers.php#64073 函数my_get_headershttp://www.php.net/manual/zh-CN/function.get-headers.php#64073被盗

Documentation for get_gheaders says: "Fetches all the headers sent by the server in response to a HTTP request". get_gheaders文档说:“获取服务器响应HTTP请求发送的所有标头”。 Should actually be: "Fetches all the headers sent by the server in response to the HTTP request that caused the current script to execute". 实际上应该是:“获取服务器发送的所有标头,以响应导致当前脚本执行的HTTP请求”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM