使用Guzzle 重写 使用curl的方法

Enviroment

php 7.3.3
laravel: 5.7
后台: laravel admin
guzzlehttp/guzzle: 6.5.8

My question:

使用Guzzle 重写 使用curl的方法。
发请求的时候,使用curl方法(request_socketpush_record)可以,但是我重写的函数(sendMessageOrigin)发不了这个请求,一直停在那里。
原因应该在curl_setopt($curl, CURLOPT_TIMEOUT, 5); 上面,但是我Guzzlehttp也加了。

   public static function request_socketpush_record(array $data = [], string $type = 'common', $is_decode = true, $data_encode = 1)
    {

        try {
            $url = config('app.host_ip') . ':' . (config('app.socket_port') - 1);
            $curl = curl_init();
            $this_header = [
                "content-type: application/x-www-form-urlencoded;charset=UTF-8;Accept’:'Application/json",
            ];
            curl_setopt($curl, CURLOPT_HTTPHEADER, $this_header);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            if (!empty($data)) {

                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_TIMEOUT, 5); //请求时间 单位秒
            $output = curl_exec($curl);
            curl_close($curl);

            RequestRecord::create([
                'type' => $type,
                'request' => json_encode($data),
                'user_id' => 0,
                'response' => json_encode($output, true),
            ]);

            if ($is_decode) {
                return json_decode($output, true);
            }

        } catch(Exception $e) {
            RequestRecord::create([
                'type' => $type,
                'request' => json_encode($data),
                'user_id' => 0,
                'response' => json_encode([$e->getMessage(),$e->getTrace()]),
            ]);
        }
    } 
    public static function sendMessageOrigin(array $data, string $type = 'common')
    {
        $url = config('app.host_ip') . ':' . (config('app.socket_port') - 1);
        $client = new Client();

        try {
            $res = $client->request('POST', $url, [
                'headers' => [
                    'Accept'=> 'Application/json',
                ],
                'form_params' => $data,
                'connect_timeout' => 5
            ]);

            RequestRecord::create([
                'type' => $type,
                'request' => json_encode($data),
                'user_id' => 0,
                'response' => $res->getBody(),
            ]);

            return $res;
        } catch(Exception $e) {
            RequestRecord::create([
                'type' => $type,
                'request' => json_encode($data),
                'user_id' => 0,
                'response' => json_encode([$e->getMessage(),$e->getTrace()]),
            ]);
        }

    }

找到原因了,当初看参数的时候,看错了,附下源码Guzzlehttp的源码

        if (isset($options['timeout'])) {
            $timeoutRequiresNoSignal |= $options['timeout'] < 1;
            $conf[CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000;
        }

        // CURL default value is CURL_IPRESOLVE_WHATEVER
        if (isset($options['force_ip_resolve'])) {
            if ('v4' === $options['force_ip_resolve']) {
                $conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4;
            } elseif ('v6' === $options['force_ip_resolve']) {
                $conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V6;
            }
        }

        if (isset($options['connect_timeout'])) {
            $timeoutRequiresNoSignal |= $options['connect_timeout'] < 1;
            $conf[CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000;
        }

我不知道我为啥用connect_timeout, 应该用timeout,我当初找的是CURLOPT_TIMEOUT 对应的参数,以为这个参数就是connect_timeout
没设 application/x-www-form-urlencoded的原因是官网写了,Use form_params for application/x-www-form-urlencoded requests, and multipart for multipart/form-data requests

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 5

Accept’:'Application/json 这个问题更大吧,你看下你curl里是怎么配置的

1年前 评论
arunfung
$client = new Client([
            'headers' => ['Request_Timeout' => 5],
            'base_uri' => '192.168.1.1:80',
        ]);
$response = $client->post('/xxxxx', ['json' => $data]);
$content = json_decode($response->getBody()->getContents(), true);

试试

1年前 评论

curl设置了application/x-www-form-urlencoded,Guzzle没设

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!