guzzle

Upload file using Guzzle 6 to API endpoint

余生颓废 提交于 2019-11-27 02:06:14
问题 I am able to upload a file to an API endpoint using Postman. I am trying to translate that into uploading a file from a form, uploading it using Laravel and posting to the endpoint using Guzzle 6. Screenshot of how it looks in Postman (I purposely left out the POST URL) Below is the text it generates when you click the "Generate Code" link in POSTMAN: POST /api/file-submissions HTTP/1.1 Host: strippedhostname.com Authorization: Basic 340r9iu34ontoeioir Cache-Control: no-cache Postman-Token:

How do you log all API calls using Guzzle 6

我的梦境 提交于 2019-11-26 16:35:10
问题 I'm trying to use guzzle 6 which works fine but I'm lost when it comes to how to log all the api calls. I would like to simply log timing, logged in user from session, url and any other usual pertinent info that has to do with the API call. I can't seem to find any documentation for Guzzle 6 that refers to this, only guzzle 3 (Where they've changed the logging addSubscriber call). This is how my current API calls are: $client = new GuzzleHttp\Client(['defaults' => ['verify' => false]]); $res

Guzzle 6: no more json() method for responses

旧时模样 提交于 2019-11-26 15:25:06
问题 Previously in Guzzle 5.3: $response = $client->get('http://httpbin.org/get'); $array = $response->json(); // Yoohoo var_dump($array[0]['origin']); I could easily get a PHP array from a JSON response. Now In Guzzle 6, I don't know how to do. There seems to be no json() method anymore. I (quickly) read the doc from the latest version and don't found anything about JSON responses. I think I missed something, maybe there is a new concept that I don't understand (or maybe I did not read correctly)

Catching exceptions from Guzzle

青春壹個敷衍的年華 提交于 2019-11-26 15:23:10
问题 I'm trying to catch exceptions from a set of tests I'm running on an API I'm developing and I'm using Guzzle to consume the API methods. I've got the tests wrapped in a try/catch block but it is still throwing unhandled exception errors. Adding an event listener as described in their docs doesn't seem to do anything. I need to be able to retrieve the responses that have HTTP codes of 500, 401, 400, in fact anything that isn't 200 as the system will set the most appropriate code based on the

Guzzlehttp - How get the body of a response from Guzzle 6?

爷,独闯天下 提交于 2019-11-26 15:06:23
I'm trying to write a wrapper around an api my company is developing. It's restful, and using Postman I can send a post request to an endpoint like http://subdomain.dev.myapi.com/api/v1/auth/ with a username and password as POST data and I am given back a token. All works as expected. Now, when I try and do the same from PHP I get back a GuzzleHttp\Psr7\Response object, but can't seem to find the token anywhere inside it as I did with the Postman request. The relevant code looks like: $client = new Client(['base_uri' => 'http://companysub.dev.myapi.com/']); $response = $client->post('api/v1

How can I use Guzzle to send a POST request in JSON?

孤街浪徒 提交于 2019-11-26 15:04:45
Does anybody know the correct way to post JSON using Guzzle ? $request = $this->client->post(self::URL_REGISTER,array( 'content-type' => 'application/json' ),array(json_encode($_POST))); I get an internal server error response from the server. It works using Chrome Postman . Michal Gallovic For Guzzle 5 & 6 you do it like this: use GuzzleHttp\Client; $client = new Client(); $response = $client->post('url', [ GuzzleHttp\RequestOptions::JSON => ['foo' => 'bar'] ]); Docs user3379466 For Guzzle <= 4 : It's a raw post request so putting the JSON in the body solved the problem $request = $this-

How can I use Guzzle to send a POST request in JSON?

拜拜、爱过 提交于 2019-11-26 03:49:31
问题 Does anybody know the correct way to post JSON using Guzzle ? $request = $this->client->post(self::URL_REGISTER,array( \'content-type\' => \'application/json\' ),array(json_encode($_POST))); I get an internal server error response from the server. It works using Chrome Postman . 回答1: For Guzzle 5 & 6 you do it like this: use GuzzleHttp\Client; $client = new Client(); $response = $client->post('url', [ GuzzleHttp\RequestOptions::JSON => ['foo' => 'bar'] ]); Docs 回答2: For Guzzle <= 4 : It's a