繁体   English   中英

将其转换为使用 Guzzle 不起作用

[英]Converting it to using Guzzle doesn't work

我有以下代码目前在 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 调用方法时我是否遗漏了什么?

这应该很简单:

$client = new \GuzzleHttp\Client();
$client->request('POST', $uri, ['form_params' => $data]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM