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

後續補充

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