stupid and brief question here,
I've been messing around with trying to get endpoints working on my website for a while now, where an action triggers an endpoint call. I would like to collect stats on call success and average response time and stuff like that, so I create a model prior to making the request, and then attempt to assign object values once the request on_stats stage is reached. The problem is, when I attempt to assign the variables from inside the request, it can't access the object, throwing a Creating default object from empty value error. Guzzle has ways to make things synchronous, using promises, but I've tried and failed to implement them after a variety of errors and attempts to debug. Is there no way to make what I'm attempting to do in the code below work? How could I access the object and assign values from within the request itself?
$call = new EndpointCall;
$call->endpoint_rel_id = $endpt->id;
// Initiate GuzzleHTTP Client
$client = new Client();
$requestQuery = $endpt->endpoint_url;
$response = $client->request('POST', $requestQuery, [
'allow_redirects' => false,
'json' => $obj,
'headers' => [
'api-secret' => $user->api_sending_secret,
'Accept' => 'application/json',
],
'synchronous' => true,
'http_errors' => false,
'on_stats' => function (TransferStats $stats) {
$call->response_time = $stats->getTransferTime();
if ($stats->hasResponse()) {
$call->response = $stats->getResponse()->getStatusCode();
}
$call->save();
}
]);
Try adding use($call) to the function declaration like this:
$call = new EndpointCall;
$call->endpoint_rel_id = $endpt->id;
// Initiate GuzzleHTTP Client
$client = new Client();
$requestQuery = $endpt->endpoint_url;
$response = $client->request('POST', $requestQuery, [
'allow_redirects' => false,
'json' => $obj,
'headers' => [
'api-secret' => $user->api_sending_secret,
'Accept' => 'application/json',
],
'synchronous' => true,
'http_errors' => false,
'on_stats' => function (TransferStats $stats) use($call) {
$call->response_time = $stats->getTransferTime();
if ($stats->hasResponse()) {
$call->response = $stats->getResponse()->getStatusCode();
}
$call->save();
}
]);
About the use keyword
Variables are not accessible inside functions unless they are declared as global. In much the same way, variables from the child scope are not accessible from within the closure unless explicitly stated using the use keyword.
Related
I have a php class that uses guzzle to call an API and get a response:
public function getResponseToken()
{
$response = $this->myGUzzleClient->request(
'POST',
'/token.php,
[
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded'
],
'form_params' => [
'username' => $this->params['username'],
'password' => $this->params['password'],
]
]
);
return json_decode($response->getBody()->getContents())->token;
}
I am trying to test this method using guzzle mock handler, this is what I have done so far but not working:
public function testGetResponseToken()
{
$token = 'stringtoken12345stringtoken12345stringtoken12345';
$mockHandler = new MockHandler([
new Response(200, ['X-Foo' => 'Bar'], $token)
]
);
$handlerStack = HandlerStack::create($mockHandler);
$client = new Client(['handler' => $handlerStack]);
$myService = new MyService(
new Logger('testLogger'),
$client,
$this->config
);
$this->assertEquals($token, $myService->getResponseToken());
}
the error I am getting says "Trying to get property of non-object", so looks to me MyService is not using the handler to make the call. What am I doing wrong?
The class works as expected outside of the test context. Also note the client in normally injected in MyService from service.yml (I am using symfony).
Your handler work fine, you just mock the wrong response data. You should make the response as raw json.
Try
$token = 'stringtoken12345stringtoken12345stringtoken12345';
$mockHandler = new MockHandler(
[
new Response(200, ['X-Foo' => 'Bar'], \json_encode([ 'token' => $token ]))
]
);
Now it should be works
Using Laravel 5 and trying to send some data from my site to another one, which provides me with the REST API. But they use cookies as a authorization. For this moment, I've passed auth successfully. And stuck on how should I send this cookie to API interface via POST method? Here is my listing.
Thanx in advance.
P.S. All things are going on inside the controller.
if (Cookie::get('amoauth') !== null) {
//COOKIE IS HERE
$client = new Client();
$newlead = $client->post('https://domain.amocrm.ru/private/api/v2/json/leads/set', [
'add' => [
'add/name' => 'TEST LEAD',
'add/date_create' => time(),
'add/last_modified' => time(),
'add/status_id' => '1',
'add/price' => 5000
]
]);
} else {
$client = new Client();
$auth = $client->post('https://domain.amocrm.ru/private/api/auth.php',[
'USER_LOGIN' => 'login',
'USER_HASH' => 'hash',
'type' => 'json'
]);
$auth = $auth->getHeaders('Set-Cookie');
Cookie::queue('amoauth', $auth, 15);
return redirect('/test');
}
Now it returns me the following:
Client error: `POST https://domain.amocrm.ru/private/api/v2/json/leads/set` resulted in a `401 Unauthorized` response.
Found the solution: switched to ixudra/curl.
I'm using guzzle 6 in laravel 5 to send a post request but I'm getting ERR_INVALID_CHUNKED_ENCODING when I try to access the request() in the method that handles the post request.
Here's my code:
Routes.php
Route::get('/guzzle', [
'as' => 'guzzle-test',
'uses' => 'TestController#getTest'
]);
Route::post('/guzzle', [
'as' => 'guzzle-post-test',
'uses' => 'TestController#postTest'
]);
TestController.php
public function getTest()
{
$client = new Client();
$data = [
'hey' => 'ho'
];
$request = $client->post(route('guzzle-post-test'), [
'content-type' => 'application/json'
], json_encode($data));
return $request;
}
public function postTest()
{
dd(getTest());
}
I getting to the post request handler since I've tried to diedump a string and it gets there, but if i call the request() I get that error. For what I've researched It may have something to with the content length, but after reading guzzle's docs and some stuff around the web I could find how to get and pass the content length appropriately in the request. Any help would be very appreciated!
First off, here's some test code which you should be able to adapt for your purposes (also see form_params in the docs for GuzzleHttp):
public function validateRecaptcha()
{
$client = new Client;
$response = $client->request('POST', 'https://www.google.com/recaptcha/api/siteverify', [
'form_params' => [
'secret' => env('RECAPTCHA_SECRET'),
'response' => Request::input('g-recaptcha-response'),
'remoteip' => Request::ip()
]
]);
return $response;
}
I just ran into the same issue and found that trying to return the response object in Laravel gave me ERR_INVALID_CHUNKED_ENCODING. Whereas, doing a dd() on the response itself showed me what I was actually wanting to see:
public function validateRecaptcha()
{
$client = new Client;
$response = $client->request('POST', 'https://www.google.com/recaptcha/api/siteverify', [
'form_params' => [
'secret' => env('RECAPTCHA_SECRET'),
'response' => Request::input('g-recaptcha-response'),
'remoteip' => Request::ip()
]
]);
dd($response);
}
Unfortunately, without doing further research, I'm unable to explain why ERR_INVALID_CHUNKED_ENCODING keeps coming up when I try to return the client library's objects to the browser, but my initial inclination is that it's a data type issue.
As far as your question goes, you're not actually trying to get back the "request" but rather the response. According to http://docs.guzzlephp.org/en/latest/quickstart.html#using-responses, if you want to get the API response contained in the response object (or at least in my case, I did), you'll want to use the getBody() method:
public function validateRecaptcha()
{
$client = new Client;
$response = $client->request('POST', 'https://www.google.com/recaptcha/api/siteverify', [
'form_params' => [
'secret' => env('RECAPTCHA_SECRET'),
'response' => Request::input('g-recaptcha-response'),
'remoteip' => Request::ip()
]
]);
return $response->getBody();
}
And then of course, if you expect it to be a JSON response (i.e. REST), then simply pass it to json_decode() to get your associative array back.
public function validateRecaptcha()
{
$client = new Client;
$response = $client->request('POST', 'https://www.google.com/recaptcha/api/siteverify', [
'form_params' => [
'secret' => env('RECAPTCHA_SECRET'),
'response' => Request::input('g-recaptcha-response'),
'remoteip' => Request::ip()
]
]);
return json_decode($response->getBody(), true); // true = assoc. array
}
Hope that helps!
My objective is to use Guzzle 6 to create a pool of asynchronous requests that PUT json data. Then monitor each $promise success/failure.
For comparison to my POOL code example, the following single request to $client->request() converts the 3rd parameter to encoded json and then adds the Content-type:application/json.**
$client = new Client([
'base_uri' => BASE_URL . 'test/async/', // Base URI is used with relative requests
'timeout' => 0, // 0 no timeout for operations and watching Promises
]);
$response = $client->request('PUT', 'cool', ['json' => ['foo' => 'bar']]);
On the receiving API endpoint, I can read the json from the single request above by doing the following:
$json = file_get_contents('php://input');
$json = json_decode($json, true);
Using the concurrent requests example in the docs, for creating a Pool of asynchronous requests using new Request(), I hoped the same parameters (method, url endpoint, json flag) could be used, as in the single $client->request() example above. However, yield new Request() does not handle the 3rd json parameter like $client->request(). What is the correct Guzzle function to call from my Pool code to set json and content-type correctly? Or is there a better way to create a large pool of asynchronous requests and monitor their outcome?
POOL code example:
$this->asyncRequests = [
[
'endpoint' => 'cool'
],
[
'endpoint' => 'awesome'
],
[
'endpoint' => 'crazy'
],
[
'endpoint' => 'weird'
]
];
$client = new Client([
'base_uri' => BASE_URL, // Base URI is used with relative requests
'timeout' => 0 // 0 no timeout for operations and watching Promises
]);
$requests = function ($asyncRequests) {
$uri = BASE_URL . 'test/async/';
foreach ($asyncRequests as $key => $data) {
yield new Request('PUT', "{$uri}{$data['endpoint']}", ['json' => ['foo' => 'bar']]);
}
};
$pool = new Pool($client, $requests($this->asyncRequests), [
'concurrency' => 10,
'fulfilled' => function ($response, $index) {
$this->handleSuccessPromises($response, $index);
},
'rejected' => function ($reason, $index) {
$this->handleFailurePromises($reason, $index);
},
]);
$promise = $pool->promise(); // Initiate the transfers and create a promise
$promise->wait(); // Force the pool of requests to complete.
Hopefully, someone else will jump in and let me know if there is a more correct way to accomplish my objective, but after looking under the hood in Guzzle I realized new Request()'s 3rd parameter was looking for header information, and the 4th parameter was looking for a body. So the following code works using the Pool.:
foreach ($syncRequests as $key => $headers) {
yield new Request('PUT', "{$uri}{$headers['endpoint']}", ['Content-type' => 'application/json'], json_encode(['json' => ['nonce' => $headers['json']]]));
}
Also in docs for Psr7\Request
If you want full control, don't use the Request() object in your Pool. Instead, start the request yourself by having your pool's generator yielding a callable function which starts the request. That gives you total control of all options. Here is a correct code example:
https://stackoverflow.com/a/40622269/5562035
I am using Guzzle (v6.1.1) in PHP to make a POST request to a server. It works fine. I am adding some logging functions to log what was sent and received and I can't figure out how to get the data that Guzzle sent to the server. I can get the response just fine, but how do I get the sent data? (Which would be the JSON string.)
Here is the relevant portion of my code:
$client = new GuzzleHttp\Client(['base_uri' => $serviceUrlPayments ]);
try {
$response = $client->request('POST', 'Charge', [
'auth' => [$securenetId, $secureKey],
'json' => [ "amount" => $amount,
"paymentVaultToken" => array(
"customerId" => $customerId,
"paymentMethodId" => $token,
"publicKey" => $publicKey
),
"extendedInformation" => array(
"typeOfGoods" => $typeOfGoods,
"userDefinedFields" => $udfs,
"notes" => $Notes
),
'developerApplication'=> $developerApplication
]
]);
} catch (ServerErrorResponseException $e) {
echo (string) $e->getResponse()->getBody();
}
echo $response->getBody(); // THIS CORRECTLY SHOWS THE SERVER RESPONSE
echo $client->getBody(); // This doesn't work
echo $client->request->getBody(); // nor does this
Any help would be appreciated. I did try to look in Guzzle sourcecode for a function similar to getBody() that would work with the request, but I'm not a PHP expert so I didn't come up with anything helpful. I also searched Google a lot but found only people talking about getting the response back from the server, which I have no trouble with.
You can do this work by creating a Middleware.
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
$stack = HandlerStack::create();
// my middleware
$stack->push(Middleware::mapRequest(function (RequestInterface $request) {
$contentsRequest = (string) $request->getBody();
//var_dump($contentsRequest);
return $request;
}));
$client = new Client([
'base_uri' => 'http://www.example.com/api/',
'handler' => $stack
]);
$response = $client->request('POST', 'itemupdate', [
'auth' => [$username, $password],
'json' => [
"key" => "value",
"key2" => "value",
]
]);
This, however, is triggered before to receive the response. You may want to do something like this:
$stack->push(function (callable $handler) {
return function (RequestInterface $request, array $options) use ($handler) {
return $handler($request, $options)->then(
function ($response) use ($request) {
// work here
$contentsRequest = (string) $request->getBody();
//var_dump($contentsRequest);
return $response;
}
);
};
});
Using Guzzle 6.2.
I've been struggling with this for the last couple days too, while trying to build a method for auditing HTTP interactions with different APIs. The solution in my case was to simply rewind the request body.
The the request's body is actually implemented as a stream. So when the request is sent, Guzzle reads from the stream. Reading the complete stream moves the stream's internal pointer to the end. So when you call getContents() after the request has been made, the internal pointer is already at the end of the stream and returns nothing.
The solution? Rewind the pointer to the beginning and read the stream again.
<?php
// ...
$body = $request->getBody();
echo $body->getContents(); // -->nothing
// Rewind the stream
$body->rewind();
echo $body->getContents(); // -->The request body :)
My solution for Laravel from 5.7:
MessageFormatter works with variable substitutions, see this: https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php
$stack = HandlerStack::create();
$stack->push(
Middleware::log(
Log::channel('single'),
new MessageFormatter('Req Body: {request}')
)
);
$client = new Client();
$response = $client->request(
'POST',
'https://url.com/go',
[
'headers' => [
"Content-Type" => "application/json",
'Authorization' => 'Bearer 123'
],
'json' => $menu,
'handler' => $stack
]
);
You can reproduce the data string created by the request by doing
$data = array(
"key" => "value",
"key2" => "value",
);
$response = $client->request('POST', 'itemupdate', [
'auth' => [$username, $password],
'json' => $data,
]);
// ...
echo json_encode($data);
This will output your data as JSON string.
Documentation at http://php.net/manual/fr/function.json-encode.php
EDIT
Guzzle has a Request and a Response class (and many other).
Request has effectively a getQuery() method that returns an object containing your data as private, same as all other members.
Also you cannot access it.
This is why I think manually encode it is the easier solution.
If you want know what is done by Guzzle, it also have a Collection class that transform data and send it in request.