简体   繁体   中英

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. 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.

http://some.example.com/niceday/foobar.php?TID= abcd where abcd is some seemingly random number.

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? I'm not sure if it's relevant but the form where the login credentials are entered uses javascript

YOu can get the response header line this : $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);

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

Documentation for get_gheaders says: "Fetches all the headers sent by the server in response to a HTTP request". Should actually be: "Fetches all the headers sent by the server in response to the HTTP request that caused the current script to execute".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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