webman2.1+swoole6+php8.1上传文件报错

renloong

问题描述

使用宝塔面板安装的php swoole6扩展,在自定义进程中使用协程,在协程中使用定时任务创建协程,在协程中使用GuzzleHttp上传文件报错

程序代码

class VideoDownload1
{
    public function onWorkerStart()
    {
        new Crontab('*/2 * * * * *', function () {
            Coroutine::create(function(){
                try {
                    $url = '';
                    $file=fopen($filePath, 'r');
                    if(!$file){
                        throw new \Exception('file not found');
                    }
                    $multipart = [
                        [
                            'name' => 'file',
                            'contents' => $file,
                        ],
                        [
                            'name' => 'user_id',
                            'contents' => '1',
                        ]
                    ];
                    $client = new Client();
                    $client = $client->request('POST', $url, [
                        'headers' => [
                            'Token' => '',
                            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36',
                        ],
                        'multipart' => $multipart,
                        'timeout' => 3000,
                    ]);
                    $response = $client->getBody()->getContents();
                    $data = json_decode($response, true);
                    if ($data['status'] == 'Success') {
                    }
                } catch (ClientException $th) {
                    throw new \Exception($th->getMessage(),$th->getCode());
                }
            });
        });
    }
}

报错信息

cURL error 0: The cURL request was retried 3 times and did not succeed. The most likely reason for the failure is that cURL was unable to rewind the body of the request and subsequent retries resulted in the same error. Turn on the debug option to see what went wrong. See https://bugs.php.net/bug.php?id=47204 for more information. (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

截图报错信息里报错文件相关代码

return [
    'VideoDownload'  => [
        'eventLoop' => Swoole::class,
        'handler'  => VideoDownload1::class,
        'count'=>1
    ]
];

操作系统及workerman/webman等框架组件具体版本

框架:Workerman/5.1.0 Webman/2.1
PHP:PHP/8.1.31 (Jit off)
Swoole:通过宝塔php扩展安装的swoole6
系统:CentOS 7.6.1810 x86_64
宝塔:9.4.0

150 3 0
3个回答

renloong

使用curl访问baidu返回的结果是:“string(161) "<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>bfe/1.0.8.18</center>
</body>
</html>
"”

class VideoDownload1
{
    public function onWorkerStart()
    {
        new Crontab('*/2 * * * * *', function () {
            Coroutine::create(function(){
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, 'https://baidu.com');
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $result = curl_exec($ch);
                var_dump(curl_error($ch));
                curl_close($ch);
                var_dump($result);
            });
        });
    }
}
  • 暂无评论
轻云蔽月

php --ri swoole 看看有没有curl-native,没有的话编译swoole的时候加个--enable-swoole-curl

renloong

已解决

问题的原因是CentOS7安装了swoole6的原因,在宝塔的安装脚本中判断了curl的版本,只有高于7560才会安装curl,CentOS7最高支持的curl版本是7290

if [ "${curl_version}" -gt '7560' ];then
    ./configure --with-php-config=/www/server/php/$version/bin/php-config --enable-openssl --enable-sockets --enable-swoole-curl 
else
    ./configure --with-php-config=/www/server/php/$version/bin/php-config --enable-openssl --enable-sockets
  • 暂无评论
×
🔝