guzzle

Guzzle - Laravel. How to make request with x-www-form-url-encoded

坚强是说给别人听的谎言 提交于 2019-12-06 03:49:50
问题 I need to integrate an API so I write function: public function test() { $client = new GuzzleHttp\Client(); try { $res = $client->post('http://example.co.uk/auth/token', [ 'headers' => [ 'Content-Type' => 'application/x-www-form-urlencoded', ], 'json' => [ 'cliend_id' => 'SOMEID', 'client_secret' => '9999jjjj67Y0LBLq8CbftgfdreehYEI=', 'grant_type' => 'client_credentials' ] ]); $res = json_decode($res->getBody()->getContents(), true); dd($res); } catch (GuzzleHttp\Exception\ClientException $e)

Translate cURL request to Guzzle

故事扮演 提交于 2019-12-06 02:15:35
问题 This question was migrated from Craft CMS Stack Exchange because it can be answered on Stack Overflow. Migrated 4 years ago . I am trying to use Guzzle instead of directly using cURL to achieve and HTTP request. How do I make this same type of request but with Guzzle? Or should I just stick to cURL? $ch = curl_init(); // Set the URL curl_setopt($ch, CURLOPT_URL, $url); // don't verify SSL certificate curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // Return the contents of the response as a

How to check if endpoint is working when using GuzzleHTTP

时光总嘲笑我的痴心妄想 提交于 2019-12-06 00:08:19
So I am working with guzzleHttp and I can get the responses that I am after and catch errors. The only problem I am having is that if the base URI is wrong, the whole script fails... how can I do some sort of checking to make sure that the endpoint is actually up? $client = new GuzzleHttp\Client(['base_uri' => $url]); You might have many issues with your query, not only that the endpoint is down. Network interface on your server can go down right at the moment of query, DNS can go down, a route to the host might not be available, a connection timeout, etc. So you definitely should be ready for

Guzzle pool in PHP application

陌路散爱 提交于 2019-12-06 00:04:59
I am trying to use Guzzle pool in PHP. But I am having difficulty in dealing with ASYNC request. Below is the code snippet. $client = new \GuzzleHttp\Client(); function test() { $client = new \GuzzleHttp\Client(); $request = $client->createRequest('GET', 'http://l/?n=0', ['future' => true]); $client->send($request)->then(function ($response) { //echo 'Got a response! ' . $response; return "\n".$response->getBody(); }); } $res = test(); var_dump($res); // echoes null - I know why it does so but how to resolve the issue. Does anybody how can I make function wait and get the correct result. If

POST Request works with Postman, but not with Guzzle

亡梦爱人 提交于 2019-12-05 06:44:50
In my Laravel application, I periodically need to POST data to an API using Guzzle. The API users a bearer token to authenticate, and requests and accepts raw json. To test, I accessed the API using Postman, and everything worked wonderfully. Postman Headers: Accept:application/json Authorization:Bearer [token] Content-Type:application/json And Postman Body: { "request1" : "123456789", "request2" : "2468", "request3" : "987654321", "name" : "John Doe" } Postman returns a 200, and a JSON object as a response. Now, when I try the same with Guzzle, I get a 200 status code, but no JSON object gets

How to bypass Laravel Exception handling

杀马特。学长 韩版系。学妹 提交于 2019-12-05 05:21:14
I have a method that checks if a user has valid Session info. This is supposed to throw an Exception, Guzzle\Http\Exception\BadResponseException but when I try to catch it : catch (Guzzle\Http\Exception\BadResponseException $e) { return false; } return true Laravel doesn't get to this code and immediately starts it's own error handling. And ideas on how to bypass Laravels own implementation and use my own Catch. EDIT: I just found out Laravel uses the same Exception handler as Symfony, so I also added the Symfony2 tag. EDIT 2: I sort of fixed the issue by disabling Guzzle exceptions and

Setting up cookies for Guzzle CookieJar

帅比萌擦擦* 提交于 2019-12-05 05:15:59
I am doing unit testing in PHP for a site that requires authentication. Authentication is cookie based, so I need to be able to put a cookie like this in the cookie jar: [ 'user_token' => '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae' ] The web application can then use this known good token for the testing data, and will be able to authenticate under testing conditions to interact with the data fixtures. Also, it must be a secure cookie, and I (obviously) need to set the domain. Problem is: I don't know how to make and set this cookie and stick it in the jar. How do you do

Posting a file to a web service with Guzzle

荒凉一梦 提交于 2019-12-05 01:39:17
问题 I've been trying for several hours to make a POST request that sends a file. First tried a simple file_get_contents() with stream context, doesn't seem to work. I never get a response back while the GET on a different URL works. I searched on the web for an HTTP client and found Guzzle which was downloaded 400k times on Packagist; I decided to try out this technology. Well documented but, alas, getting an error also when posting that damn file. $request = $client ->post('/files/') -

Limit connecting time with Guzzle HTTP PHP client

橙三吉。 提交于 2019-12-05 01:26:54
I'm using Guzzle to open a list of url-s and get the headers. Some of the urls are taking too long to respond and can't be openned and i want to ignore them. It takes me up to 20+ seconds before Guzzle throws an exception and i want to change this and limit the time for connecting to 2 sec. I have this code but it still takes much longer: <?php include 'vendor/autoload.php'; $start = new \DateTime("now"); $start = $start->format("d.m.Y H:i:s"); echo $start."\n"; $client = new Guzzle\Http\Client(); Guzzle\Http\StaticClient::mount(); try { $request = $client->get('http://takestoolongexample', []

Not getting expected response from Guzzle

﹥>﹥吖頭↗ 提交于 2019-12-04 22:47:30
I'm trying to build an endpoint that forwards the data passed to it to an API using the Slim PHP Framework and I'm having trouble getting my response from a Guzzle request. $app->map( '/api_call/:method', function( $method ) use( $app ){ $client = new GuzzleHttp\Client([ 'base_url' => $app->config( 'api_base_url' ), 'defaults' => [ 'query' => [ 'access_token' => 'foo' ], ] ]); $request = $client->createRequest( $app->request->getMethod(), $method, [ 'query' => $app->request->params() ]); var_dump( $client->send( $request )->getBody() ); })->via( 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' )-