😥关于redis消费者的问题😥

青鱼

我是用了百度地图的API来获取一些信息。

突然发现消费者中无法使用workerman/http-client

2025-10-05 14:40:03 失败原因:Call to a member function getBody() on null
2025-10-05 14:40:13 失败原因:Call to a member function getBody() on null
2025-10-05 14:40:18 失败原因:Call to a member function getBody() on null
2025-10-05 14:40:18 失败原因:Call to a member function getBody() on null

然后换成了Curl就可以了

/www/wwwroot/webman/app/queue/redis/iprecord/IpRecordConsumer.php updated and reload
Workerman[start.php] reloading
请求的原始返回结果为:
{"status":0,"address":"CN|北京市|北京市|海淀区|None|100|87|76","content":{"address":"北京市海淀区","address_detail":{"adcode":"110108","city":"北京市","city_code":131,"district":"海淀区","province":"北京市","street":"","street_number":""},"point":{"x":"116.30478161520548","y":"39.96612834175736"}}}
请求的原始返回结果为:
{"status":0,"address":"CN|北京市|北京市|海淀区|None|100|87|76","content":{"address":"北京市海淀区","address_detail":{"adcode":"110108","city":"北京市","city_code":131,"district":"海淀区","province":"北京市","street":"","street_number":""},"point":{"x":"116.30478161520548","y":"39.96612834175736"}}}

至于为啥会想到换Curl是因为https://www.workerman.net/q/6409

下面大佬的评论推动我去试一试,结果真的可以!!!

但是问题来了🤔🤔🤔🤔

我用workerman/http-client没有用回调模式。为啥会无法在消费者中执行呢?正常接口却可以!!!🤔🤔🤔

下面是我http-client的代码:

<?php

declare(strict_types=1);

namespace app\common\service;

use Workerman\Http\Client;

class GetIpSevice
{
    /**
     * 获取用户IP信息
     * @param string $ip 请求对象
     * @return array 用户IP信息(国家、省份、城市)
     */
    public static function getip(): array
    {
        $options = [
            'max_conn_per_addr' => 128,   // 单个目标地址的最大并发连接数
            'keepalive_timeout' => 15,    // 保持连接超时时间(秒)
            'connect_timeout'   => 5,     // 连接阶段最多等 5 秒(连不上就放弃)
            'timeout'           => 2,     // 连上后等响应最多 3 秒(没响应就超时)
        ];
        $http = new  Client($options);
        $ak = '8i3AaJs6xEBsqsI8qbLwTaFSoxKYpRNX';
        $backUrl = 'https://api.map.baidu.com/location/ip?ak=' . $ak . '&ip=' . '114.132.120.63';
        $headers = [
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36',
            'Host' => 'api.map.baidu.com',
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'Accept-Language' => 'zh-CN,zh;q=0.9',
            'Connection' => 'keep-alive',
            'Sec-Ch-Ua' => '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
            'Sec-Ch-Ua-Platform' => '"Windows"',
        ];
        try {
            $response = $http->request($backUrl, [
                'method' => 'GET',
                'version' => '1.1',
                'headers' => $headers,
            ]);
            $json = $response->getBody();
            $ipInfo = json_decode((string)$json, true);
            return $ipInfo;
        } catch (\Exception $e) {
            return ['error' => '未查询到IP信息'];
        }
    }
}

有木有大佬知道这是啥原因的???

212 2 0
2个评论

lsmir2

更新了。

  • 暂无评论
efnic

异步执行的队列里面,就不要再用异步了。

  • 暂无评论

青鱼

330
积分
0
获赞数
0
粉丝数
2025-08-25 加入
🔝