我正在尝试将现有的phpCurl请求转换为最新的guzzle并且没有快速到达。
这是当前的请求。
$curl_opts = array(
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array('Content-Type: text/json', 'Content-length: '.strlen($json)),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $json,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://the-domainservice.php',
CURLOPT_VERBOSE => false,
CURLOPT_SSLCERT => '/path/to/file.pem',
CURLOPT_SSLCERTTYPE => 'pem',
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1
);
$curl = curl_init();
curl_setopt_array($curl, $curl_opts);
$response = curl_exec($curl);
对于Guzzle,我尝试了很多方法,但这里有几个例子。
$response = $this->client->post('https://the-domainservice.php', [
'body' => $postData,
'cert' => '/path/to/file.pem',
'config' => [
'curl' => [
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true
]
]
]
);
更详细的
$request = $this->client->createRequest('POST', 'https://the-domainservice.php', [
'cert' => '/path/to/file.pem',
'verify' => false,
'headers' => [
'Content-Type' => 'text/json',
'Content-length' => strlen(json_encode($postData))
]
]);
$postBody = $request->getBody();
foreach ($postData as $key => $value) {
$postBody->setField($key, $value);
}
$response = $this->client->send($request);
对于我的guzzle请求我只是
GuzzleHttp \ Exception \ ServerException:服务器错误响应[url] https://the-domainservice.php [状态代码] 500 [原因短语]内部服务错误
真的希望有人可以提供建议。
答案 0 :(得分:1)
现在觉得有点蠢。
只需要发送json数据并且一切正常,最终结果是。
$response = $this->client->post('https://the-domainservice.php', [
'body' => json_encode($postData),
'cert' => '/path/to/file.pem',
]
);
优秀的东西Guzzle!