我使用PHP与狂饮。我有这样的代码:
$client = new Client();
$request = new \GuzzleHttp\Psr7\Request('POST', 'http://localhost/async-post/tester.php',[
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
'form_params' => [
'action' => 'TestFunction'
],
]);
$promise = $client->sendAsync($request)->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
$promise->wait();
出于某种原因,狂饮不发送POST参数。任何建议?
谢谢 :)
我看到两两件事。参数必须去为字符串(json_encode
)而你也被包括其中作为标题,而不是身体的一部分。
然后,添加一个函数来处理响应,ResponseInterface
$client = new Client();
$request = new Request('POST', 'https://google.com', ['Content-Type' => 'application/x-www-form-urlencoded'], json_encode(['form_params' => ['s' => 'abc',] ]));
/** @var Promise\PromiseInterface $response */
$response = $client->sendAsync($request);
$response->then(
function (ResponseInterface $res) {
echo $res->getStatusCode() . "\n";
},
function (RequestException $e) {
echo $e->getMessage() . "\n";
echo $e->getRequest()->getMethod();
}
);
$response->wait();
在这个测试谷歌与客户端错误响应:POST https://google.com
导致了405 Method Not Allowed
但是,就可以了。谷歌不接受要求这样。