目前呼叫中心项目中有转码、CDR
、拨号计划、SIP
分机等模块交给 PHP
大家族中风格清奇的 Swoole
来实现,为了图方便,具体用的是 EasySwoole
框架。这次在开发 IVR
模块中要用到 WEB
服务器上传录制好的音频文件到跑在 EasySwoole
的语音服务器上,于是用到了之前博客中的 Curl
上传文件,不过这次接收上传对象由 EasySwoole
来执行。
和常用的接收文件 $_FILES
不一致的是, Swoole
都封装到了 request
对象中,在 EasySwoole
中对应的就是通过 $this->request()->getSwooleRequest()
获取请求对象,具体的可以看看下面的代码实例
<?php
namespace App\HttpController;
use EasySwoole\Config;
use EasySwoole\Core\Http\AbstractInterface\Controller;
/**
* Class Index
* @package App\HttpController
*/
class Video extends Controller
{
function index()
{
/**
* "files": {
"video": {
"name": "defmusic.wav",
"type": "audio/wave",
"tmp_name": "/tmp/swoole.upfile.zpZjtJ",
"error": 0,
"size": 1939376
}
}
*/
$swooleRequest = $this->request()->getSwooleRequest();
try {
if ($swooleRequest->files) {
$filename = basename($swooleRequest->files['video']['name']);
$tmpname = $swooleRequest->files['video']['tmp_name'];
if (move_uploaded_file($tmpname, Config::getInstance()->getConf('video_path') . $filename)) {
$this->writeJson(['status' => 1, 'msg' => 'success']);
} else {
$this->writeJson(['status' => 0, 'msg' => 'failure']);
}
} else {
$this->writeJson(['status' => 0, 'msg' => 'no file']);
}
} catch (\Exception $e) {
$this->writeJson(['status' => 0, 'msg' => $e->getMessage()]);
}
}
}