Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default header in Guzzle?

Tags:

php

guzzle

$baseUrl = 'http://foo'; $config = array(); $client = new Guzzle\Http\Client($baseUrl, $config); 

What's the new way to set the default header for Guzzle without passing it as a parameter on every $client->post($uri, $headers)?

There's $client->setDefaultHeaders($headers) but it's deprecated.

setDefaultHeaders is deprecated. Use the request.options array to specify default request options 
like image 722
Jürgen Paul Avatar asked Jul 20 '13 20:07

Jürgen Paul


People also ask

What is GuzzleHttp guzzle?

Guzzle, PHP HTTP client. Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...

How do I debug guzzle request?

Debugging when using Guzzle, is quiet easy by providing the debug key in the payload: $client->request('GET', '/url, ['debug' => true]); This is quiet easy and not an issue if your are not passing any body content, using only query string to dump what's been request.

How do I set timeout on guzzle?

If you have a queued job that uses Guzzle to make a request, make sure you set a timeout value for the guzzle request: (new \GuzzleHttp\Client())->get('http://domain.com', [ 'timeout' => 15 ]);

How do I get my status code from response guzzle?

You can use the getStatusCode function. $response = $client->request('GET', $url); $statusCode = $response->getStatusCode(); Note: If your URL redirects to some other URL then you need to set false value for allow_redirects property to be able to detect initial status code for parent URL.


1 Answers

If you are using Guzzle v=6.0.*

$client = new GuzzleHttp\Client(['headers' => ['X-Foo' => 'Bar']]); 

read the doc, there are more options.

like image 115
tasmaniski Avatar answered Sep 19 '22 17:09

tasmaniski