Php Guzzle不发送post请求
我正在使用PHP和Guzzle。 我有以下代码:Php Guzzle不发送post请求,php,guzzle,Php,Guzzle,我正在使用PHP和Guzzle。 我有以下代码: $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' => [
$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();
出于某种原因,Guzzle不发送POST参数。
有什么建议吗
谢谢:我看到两件事。
参数必须作为字符串json_encode
你也把它们作为标题的一部分,而不是主体
然后我添加了一个函数,将响应作为ResponseInterface处理
在这个测试中,谷歌用一个
客户端错误:POSThttps://google.com 导致不允许使用405方法
但是没关系。Google不接受这样的请求。是否严格要求异步发送请求?是的,必须是异步的
$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();