2012-03-12 63 views
0

我正在編寫一個向PHP CLI發送請求的應用程序。但是,當我嘗試運行該應用程序時,php抱怨找不到要運行的腳本。如何從C調用PHP CLI?

下面是相關代碼:

char *params[] = { 
    "/usr/bin/php", 
    "-f /var/www/test/php/tracker/gps-upload-sh.php", 
    imei, 
    rmc, 
    (char *) 0 
}; 
signal(SIGCHLD, SIG_IGN); 
pid_t pID = fork(); 
if (pID == 0) { 
    if(execv("/usr/bin/php", params) == -1) { 
     perror("Failed to call php."); 
     _exit(1); 
    } 
    _exit(0); 
} 

輸出:

$ ./socket 
Could not open input file: /var/www/test/php/tracker/gps-upload-sh.php 
^C 

文件:

$ ls -l /var/www/test/php/tracker/gps-upload-sh.php 
-rwxr-xr-x 1 onik onik 6707 2012-03-09 16:00 /var/www/test/php/tracker/gps-upload-sh.php 

直接運行(其中REQUIRED_PARAMS是一樣從execv通過的) :

$ php -f /var/www/test/php/tracker/gps-upload-sh.php [REQUIRED_PARAMS] 
OK 

怎麼辦?

回答

3

嘗試分離-f和路徑到單獨的參數:

char *params[] = { 
    "/usr/bin/php", 
    "-f", 
    "/var/www/test/php/tracker/gps-upload-sh.php", 
    imei, 
    rmc, 
    (char *) 0 
}; 

(這是你的shell做什麼你把兩者之間有一個空格)


另外,取出-f與路徑之間的空格:

char *params[] = { 
    "/usr/bin/php", 
    "-f/var/www/test/php/tracker/gps-upload-sh.php", 
    imei, 
    rmc, 
    (char *) 0 
}; 
+0

謝謝,第一個建議成功了!我懷疑這真的很簡單,完全錯過了:) – onik 2012-03-12 08:36:20