[英]Converting it to using Guzzle doesn't work
I have the following code that is currently used on an SDK for an integration and I want to migrate this to using Guzzle PHP for a cleaner and simplified code.我有以下代码目前在 SDK 上用于集成,我想将其迁移到使用 Guzzle PHP 以获得更清晰和简化的代码。
<?php
class Request
{
private $is_last_session_id;
public function send($s_url, $data)
{
$params = [
'http' => [
'method' => 'POST',
'content' => $data,
'header' => "Content-type: application/x-www-form-urlencoded",
],
];
$ctx = stream_context_create($params);
ini_set('user_agent', 'PHP Client /5.2.00');
$fp = fopen($s_url, 'rb', false, $ctx);
$response = stream_get_contents($fp);
$meta = stream_get_meta_data($fp);
foreach (array_keys($meta) as $h) {
$v = $meta[$h];
if (is_array($v)) {
foreach (array_keys($v) as $hh) {
$vv = $v[$hh];
if (is_string($vv) && substr_count($vv, 'JSESSIONID')) {
$this->is_last_session_id = substr($vv, strpos($vv, '=') + 1, 24);
}
}
}
}
return $response;
}
}
Here is the code that uses Guzzle这是使用 Guzzle 的代码
private function guzzleRequest($uri, $data)
{
$client = new \GuzzleHttp\Client();
$client->request('POST', $uri, [
'form_params' => $data,
'headers' => [
'Content-type' => 'application/x-www-form-urlencoded',
],
]);
}
From the code, I understand that it is doing a POST request, but when I try to use Guzzle's $client->post()
or $client->request('POST',$data)
with the specific headers as per the code above, I get a 500 server error.从代码中,我了解到它正在执行 POST 请求,但是当我尝试根据代码使用 Guzzle 的$client->post()
或$client->request('POST',$data)
和特定标头时上面,我收到一个 500 服务器错误。 The above code as is works fine.上面的代码原样工作正常。 Am I missing something when calling the methods using Guzzle?使用 Guzzle 调用方法时我是否遗漏了什么?
This should be as simple as:这应该很简单:
$client = new \GuzzleHttp\Client();
$client->request('POST', $uri, ['form_params' => $data]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.