使用寶塔面板安裝的php swoole6擴(kuò)展,在自定義進(jìn)程中使用協(xié)程,在協(xié)程中使用定時(shí)任務(wù)創(chuàng)建協(xié)程,在協(xié)程中使用GuzzleHttp上傳文件報(bào)錯(cuò)
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/5.1.0 Webman/2.1
PHP:PHP/8.1.31 (Jit off)
Swoole:通過寶塔php擴(kuò)展安裝的swoole6
系統(tǒng):CentOS 7.6.1810 x86_64
寶塔:9.4.0
使用curl訪問baidu返回的結(jié)果是:“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);
});
});
}
}
已解決
問題的原因是CentOS7安裝了swoole6的原因,在寶塔的安裝腳本中判斷了curl的版本,只有高于7560才會(huì)安裝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