是否有可能让Guzzle池等待请求?
现在我可以动态地向池中添加请求,但是一旦池为空,guzzle就会停止(显然).
当我同时处理10个左右的页面时,这是一个问题,因为我的请求数组将为空,直到处理结果HTML页面并添加新链接.
这是我的发电机:
$generator = function () {
while ($request = array_shift($this->requests)) {
if (isset($request['page'])) {
$key = 'page_' . $request['page'];
} else {
$key = 'listing_' . $request['listing'];
}
yield $key => new Request('GET', $request['url']);
}
echo "Exiting...\n";
flush();
};
Run Code Online (Sandbox Code Playgroud)
我的游泳池:
$pool = new Pool($this->client, $generator(), [
'concurrency' => function() {
return max(1, min(count($this->requests), 2));
},
'fulfilled' => function ($response, $index) {
// new requests may be added to the $this->requests array here
}
//...
]);
$promise = …Run Code Online (Sandbox Code Playgroud) 当我使用Postman进行API调用时,我会收到一个JSON对象.这就是我所期望的.
但是当我像Guzzle一样打电话时:
$client = new \GuzzleHttp\Client(['base_uri' => 'https://api.dev/']);
$response = $client->request('GET', 'search', [
'verify' => false,
]);
var_dump($response->getBody()); //null
var_dump($response); //returns below
Run Code Online (Sandbox Code Playgroud)
我从Guzzle下面转储
Response {#304 ?
-reasonPhrase: "OK"
-statusCode: 200
-headers: array:8 [?
"Server" => array:1 [?]
"Content-Type" => array:1 [?
0 => "application/json"
]
"Transfer-Encoding" => array:1 [?]
"Connection" => array:1 [?]
"Cache-Control" => array:1 [?]
"Date" => array:1 [?]
"X-RateLimit-Limit" => array:1 [?]
"X-RateLimit-Remaining" => array:1 [?]
]
-headerNames: array:8 [?
"server" => "Server"
"content-type" => "Content-Type"
"transfer-encoding" …Run Code Online (Sandbox Code Playgroud) 我正在尝试为名为creditguard的本地网关创建一个自定义的omnipay驱动程序.对于此网关,您需要将数据发布到端点并返回付款表单的重定向URL.
我的问题是你如何在购买前发布并获得回复?
编辑:
Gateway.php
class Gateway extends AbstractGateway
{
public function getName()
{
return 'Creditguard';
}
public function getDefaultParameters()
{
return array();
}
public function getEndpoint()
{
return 'https://verifonetest.creditguard.co.il/xpo/Relay';
}
public function purchase(array $parameters = array())
{
return $this->createRequest('\Nirz\Creditguard\Message\PurchaseRequest', $parameters);
}
public function completePurchase(array $parameters = array())
{
return $this->createRequest('\Nirz\Creditguard\Message\CompletePurchaseRequest', $parameters);
}
}
Run Code Online (Sandbox Code Playgroud)
PurchaseRequest.php
class PurchaseRequest extends AbstractRequest
{
protected $liveEndpoint = 'https://verifonetest.creditguard.co.il/xpo/Relay';
protected $testEndpoint = 'https://verifonetest.creditguard.co.il/xpo/Relay';
public function getData()
{
$this->validate('amount');
// Either the nodifyUrl or the returnUrl can …Run Code Online (Sandbox Code Playgroud) 有没有办法在Guzzle中模拟响应和请求?
我有一个发送一些请求的类,我想测试.
在Guzzle 文档中,我找到了一种方法,如何模拟响应和单独请求.但我怎样才能将它们结合起来?
因为,如果使用历史堆栈,guzzle试图发送一个真实的请求.和签证一样,当我模拟响应处理程序无法测试请求时.
class MyClass {
public function __construct($guzzleClient) {
$this->client = $guzzleClient;
}
public function registerUser($name, $lang)
{
$body = ['name' => $name, 'lang' = $lang, 'state' => 'online'];
$response = $this->sendRequest('PUT', '/users', ['body' => $body];
return $response->getStatusCode() == 201;
}
protected function sendRequest($method, $resource, array $options = [])
{
try {
$response = $this->client->request($method, $resource, $options);
} catch (BadResponseException $e) {
$response = $e->getResponse();
}
$this->response = $response;
return $response;
}
}
Run Code Online (Sandbox Code Playgroud)
测试:
class MyClassTest …Run Code Online (Sandbox Code Playgroud) 我需要集成一个API,所以我要编写函数:
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) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
}
}
Run Code Online (Sandbox Code Playgroud)
作为回应,我收到消息:
{"data":{"error":"invalid_clientId","error_description":"ClientId should be sent."}}
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试在POSTMAN应用程序中使用相同的数据运行相同的URL时,我得到了正确的结果:
我的代码有什么不好?我form_params也发送正确的尝试将form_params更改为json,但再次出现相同的错误...
如何解决我的问题?
我试图卷曲到我的本地主机 laravel 站点。我正在使用 XAMPP 7.1 当我尝试重新连接此代码时,总是出现错误(而不是200 OK响应)
致命错误:未捕获的 GuzzleHttp\Exception\ConnectException:cURL 错误 6:无法解析主机:test.example.localhost(参见http://curl.haxx.se/libcurl/c/libcurl-errors.html) 在 D:\xamp7.1\htdocs\wittymanager\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php:185 堆栈跟踪:#0 D:\xamp7.1\htdocs\wittymanager\vendor\guzzlehttp\guzzle\ src\Handler\CurlFactory.php(149): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array) #1 D:\xamp7.1\htdocs\wittymanager\vendor\guzzlehttp\guzzle\ src\Handler\CurlFactory.php(102): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #2D :\xamp7.1\htdocs\wittymanager\vendor\guzzlehttp\guzzle\src\Handler\CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler) \EasyHandle), 对象(GuzzleHttp\Handler\CurlFactory)) #3 D:\xamp7。1\htdocs\wittymanager\vendor\guzzlehttp\guzzle in D:\xamp7.1\htdocs\wittymanager\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 185
// URL = http://test.example.localhost/api/lang-sync
public static function test($url, $data) {
$client = new \GuzzleHttp\Client();
$res = $client->request('POST', $url, $data);
$response = $res->getBody();
if($res->getStatusCode() == 200)
{
print_r($res);
} else {
print_r($response);
}
}
Run Code Online (Sandbox Code Playgroud)
在登台服务器上没问题。重新启动 xampp 没有帮助。
编辑:
test.example.localhost 已添加到主机文件中。 …
我已经安装了客户端,并使用composer dump autoload进行了更新,但我仍然遇到了同样的错误.在通过composer安装后,需要在项目目录中使用guzzlehttp/guzzle:~6.0.
$client = new GuzzleHttp\Client();
Run Code Online (Sandbox Code Playgroud)
为什么它没有工作,为什么它甚至引用了错误的目录?
我正在构建一个小应用程序Laravel 5.5,我正在使用它Guzzle Http来调用api url并获得响应.很少有api调用具有某些条件,有标题可以作为生成请求的授权.我正在尝试将标题放置如下:
public function post(Request $request)
{
try {
if ($request->url_method == 'get') {
$request = $this->client->get($request->url, [ 'headers' => [ 'access_token' => $request->headers->access_token]]);
}
else if ($request->url_method == 'post')
{ $request = $this->client->post($request->url, [$request->request_data]); }
else { return response()->json(['data' => 'Invalid Method'],500); }
$response = \GuzzleHttp\json_decode($request->getBody());
return response()->json(['data' => json_decode($response->d)], $request->getStatusCode());
}
catch (ClientException $e) {
return response()->json(['data' => 'Invalid Request.'], $request->getStatusCode());
}
}
Run Code Online (Sandbox Code Playgroud)
但这给了我错误:
未定义的属性:Symfony\Component\HttpFoundation\HeaderBag :: $ access_token
请查看截图:
此外,当在控制台中通过浏览器进行调用时,它给出了同样的错误:
请帮帮我,谢谢.
Guzzle http 正在截断超过 120 个字符的异常,但我需要记录完整的异常消息。我怎样才能做到这一点?
我正在使用 Laravel 4.2.22。
我尝试GuzzleHttp在本地使用并向本地 URL 发出请求,但收到了Operation timed out. 使用的代码片段:
$url = 'https://boot-vue.test'; //local domain, tried with other urls as well
$client = new \GuzzleHttp\Client([
'verify' => false,
'timeout' => 5, // Response timeout
'connect_timeout' => 5, // Connection timeout
'peer' => false
]);
$response = $client->request($method, $url, [
'json' => $data,
'headers' => $headers,
]);
Run Code Online (Sandbox Code Playgroud)
上面的代码段适用于任何公共网站。
任何帮助都是可观的!
guzzlehttp ×10
php ×8
guzzle ×5
laravel ×5
curl ×2
composer-php ×1
guzzle6 ×1
laravel-5 ×1
laravel-5.5 ×1
multi-tenant ×1
omnipay ×1
postman ×1
unit-testing ×1