我正在编写一个向PHP CLI发送请求的应用程序。但是,当我尝试运行该应用程序时,php抱怨它无法找到要运行的脚本。
以下是相关代码:
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
怎么办?
答案 0 :(得分: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
};