Getting initial headers with Guzzle Http Client

I have a URL ( http://forexample.com/index ) where I am sending the raw header:

header("HTTP/1.1 401 Some message");

      

With Guzzle, I want to get this message with the original header. Unfortunately, after the request completes, I cannot find this message between the headers:

$client = new GuzzleHttp\Client();

try {
    $res = $client->post( 'http://forexample.com/index' );
} catch ( GuzzleHttp\Exception\BadResponseException $e ) {
    // On HTTP response other than 200 Guzzle throws an exception
    $res = $e->getResponse();
}

var_dump( $res->getHeaders() );

      

Let's say if I call this URL with my own PHP function get_headers

:

var_dump( get_headers( 'http://forexample.com/index' ) );

      

I get all the headers. So, any ideas?

+3


source to share


1 answer


Guzzle Has predefined status messages for various codes. See here .

So your message will be replaced with this message based on the code you sent. And the default message can be received,

$res->getReasonPhrase();

      

UPDATE

I know why we are not getting the "Some message" with a function $res->getReasonPhrase();

. Problem (?) Lies here on line 76.

isset($startLine[2]) ? (int) $startLine[2] : null

      



The above line $startLine[2]

is 'Some message'

where you specified in the title, but because of int

, the result 0

and because of the following code snippet is replaced with the default message here .

if (!$reason && isset(self::$phrases[$this->statusCode])) {
    $this->reasonPhrase = self::$phrases[$status];
} else {
    $this->reasonPhrase = (string) $reason;
}

      

I'm sure there must be some reason for t25>, but I created a pull request replacing it int

with string

just to know if there is some reason. I hope they deny my pull request and tell me why I'm wrong and the int

casting is really correct.

UPDATE

The capture request is accepted and merged with the master. Therefore, this bug would be fixed in a future version.

+2


source







All Articles