将其转换为使用Guzzle无效

问题描述 投票:0回答:1

我具有以下当前在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;
    }
}

这里是使用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',
            ],
        ]);
    }

[从代码中,我知道它正在执行POST请求,但是当我尝试根据上面的代码将Guzzle的$client->post()$client->request('POST',$data)与特定的标头一起使用时,出现500服务器错误。上面的代码是正常的。使用Guzzle调用方法时是否缺少某些内容?

php http fopen guzzle
1个回答
0
投票

这应该很简单:

$client = new \GuzzleHttp\Client();
$client->request('POST', $uri, ['form_params' => $data]);
© www.soinside.com 2019 - 2024. All rights reserved.