php执行Linux命令/或者C语言程序

1.应用场景

有时要在服务器(Linux)中处理一些需求, 如文件处理, 将ini文件处理为json文件,需要写个程序, 直接运行在Linux中, 很自然使用C语言程序来编写.

2.核心代码

/**
 * [CAN_POST]
 * just test to get score [Android]
 */
public function getAndroidScore($params = [], $os = 'Android')
{
    $params = ['Mali-G72', 'OpenGL ES 3.2 V1', '9', 'HUAWEI', '123', '4', '4', '2000'];//测试值
    $jsonFile = ROOT_PATH . '/temp/retjson.json';//中间临时文件,用于存放json数据,建议根据项目来使用多个中间临时文件
    $iniFile = ROOT_PATH . '/JsonINIConverter/BaseDeviceProfiles.ini';//需要根据项目存到服务器一份,这里是测试文件,建议放在ROOT_PATH . '/temp/base.ini'
    $command = ROOT_PATH . "/JsonINIConverter toDeviceProfile {$os} {$iniFile} {$jsonFile} \"{$params[0]}\" \"{$params[1]}\" \"{$params[2]}\" \"{$params[3]}\" \"{$params[4]}\" \"{$params[5]}\" \"{$params[6]}\" \"{$params[7]}\"";
    echo $command . PHP_EOL;
    $res = exec($command, $output, $return_var);
    echo $res . PHP_EOL;
    if ($res !== 'completed') {
        throw new CustomException('Run Command Error: ' . $res . ', ' . (is_array($output) ?implode("",$output):$output) . ', ' . $return_var);
    }
    if (!file_exits($jsonFile)) {
        throw new CustomException('文件不存在!');
    }
    if (is_readable($jsonFile)){
        $json = file_get_contents($jsonFile);
    } else {
        throw new CustomException('文件无可读权限!');
    }
    exit($json);
    return json_decode($json, 1);
}

3.问题

TBD

4.参考

TBD

后续补充

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章