从 Guzzle on_stats 中为对象赋值
Assign value to object from within Guzzle on_stats
愚蠢而简短的问题,
一段时间以来,我一直在尝试让端点在我的网站上运行,其中一个操作会触发一个端点调用。我想收集有关呼叫成功和平均响应时间以及类似内容的统计信息,因此我在发出请求之前创建了一个模型,然后在达到请求 on_stats 阶段后尝试分配对象值。问题是,当我尝试从请求内部分配变量时,它无法访问该对象,从而引发 Creating default object from empty value
错误。 Guzzle 有一些方法可以使用 promises 使事情同步,但在出现各种错误并尝试调试后,我尝试并未能实现它们。有没有办法让我在下面的代码中尝试做的事情起作用?我如何访问对象并从请求本身中分配值?
$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();
}
]);
尝试将 use($call)
添加到函数声明中,如下所示:
$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();
}
]);
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.
愚蠢而简短的问题,
一段时间以来,我一直在尝试让端点在我的网站上运行,其中一个操作会触发一个端点调用。我想收集有关呼叫成功和平均响应时间以及类似内容的统计信息,因此我在发出请求之前创建了一个模型,然后在达到请求 on_stats 阶段后尝试分配对象值。问题是,当我尝试从请求内部分配变量时,它无法访问该对象,从而引发 Creating default object from empty value
错误。 Guzzle 有一些方法可以使用 promises 使事情同步,但在出现各种错误并尝试调试后,我尝试并未能实现它们。有没有办法让我在下面的代码中尝试做的事情起作用?我如何访问对象并从请求本身中分配值?
$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();
}
]);
尝试将 use($call)
添加到函数声明中,如下所示:
$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();
}
]);
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.