简体   繁体   English

从shell或apache服务器运行PHP脚本

[英]Run PHP script from shell or apache server

I am wondering if a PHP script can be executed from a shell command line. 我想知道是否可以从shell命令行执行PHP脚本。

Does a script executed from shell have the exact functionality if executed from the browser? 从shell执行的脚本是否具有从浏览器执行的确切功能? or is there a difference in coding. 或者编码有什么不同。

Is it better to run a script from shell for performance and also is it better to run it from windows or unix/linux 从shell运行脚本以提高性能是否更好?从windows或unix / linux运行它更好

I am asking all these questions because, I am suppose to develop a PHP script that can fetch some data from http headers of some urls listed in a MySQL db and then store the data in the database. 我问所有这些问题因为,我想开发一个PHP脚本,可以从MySQL数据库中列出的一些URL的http头中获取一些数据,然后将数据存储在数据库中。

Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows? 你能指点我正确的方向,我需要ubuntu,还是有一个可以从windows运行php的shell? all I have at the moment installed is WAMP, which has mysql, php and apache server. 我现在所有安装的都是WAMP,它有mysql,php和apache服务器。

I am sorry for being a novice. 我很抱歉是新手。

thanks for your kind help 谢谢你的帮助

I am wondering if a PHP script can be executed from a shell command line. 我想知道是否可以从shell命令行执行PHP脚本。

It's possible either by executing: 它可以通过执行:

$ php -f your_script.php

Or by inserting #/usr/bin/env php into the first line of the script and by making it executable. 或者将#/usr/bin/env php插入脚本的第一行并使其可执行。

$ head -n 1 your_sript.php
#/usr/bin/env php
$ chmod +x your_script.php
$ ./your_script.php

Note: this example only works on UNIX systems. 注意:此示例仅适用于UNIX系统。

Does a script executed from shell have the exact functionality if executed from the browser? 从shell执行的脚本是否具有从浏览器执行的确切功能? or is there a difference in coding. 或者编码有什么不同。

You can use the same Syntax/Functions etc. The only difference is that there are command line arguments in $argv and some other values in the $_SERVER variable. 您可以使用相同的语法/函数等。唯一的区别是$argv中有命令行参数, $_SERVER变量中有一些其他值。

Is it better to run a script from shell for performance and also is it better to run it from windows or unix/linux. 从shell运行脚本以提高性能是否更好?从windows或unix / linux运行它更好。

That doesn't really matter. 这并不重要。 For your usecase you don't really need a webserver, and a full featured GUI. 对于您的用例,您不需要Web服务器和全功能GUI。 The advantage of having a command line tool is, you can combine your program with other program available like grep etc. 使用命令行工具的优点是,您可以将程序与grep等其他可用程序结合使用。

Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows? 你能指点我正确的方向,我需要ubuntu,还是有一个可以从windows运行php的shell?

You don't need ubuntu, you can also execute a shell script from windows. 你不需要ubuntu,你也可以从windows执行shell脚本。 The PHP executable is located in the %PATH% . PHP可执行文件位于%PATH% This question will help you in order to do this: https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them 这个问题将帮助您做到这一点: https//superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use -他们

Then simply open cmd.exe and execute a script using php -f your_script.php 然后只需打开cmd.exe并使用php -f your_script.php执行脚本

I am wondering if a PHP script can be executed from a shell command line. 我想知道是否可以从shell命令行执行PHP脚本。

Yes

Does a script executed from shell have the exact functionality if executed from the browser? 从shell执行的脚本是否具有从浏览器执行的确切功能? or is there a difference in coding. 或者编码有什么不同。

It won't have $_REQUEST and friends populated, and $_SERVER won't have server information in it. 它不会有$_REQUEST和朋友填充, $_SERVER也不会有服务器信息。

Is it better to run a script from shell for performance 从shell运行脚本以提高性能是否更好?

Maybe. 也许。 It avoids the overhead of runnning a webserver. 它避免了运行Web服务器的开销。 It stops you having cached versions in memory for faster re-execution. 它会阻止您在内存中缓存版本以便更快地重新执行。

and also is it better to run it from windows or unix/linux 而且从windows或unix / linux运行它也更好

Benchmark it. 基准吧。

I am asking all these questions because, I am suppose to develop a PHP script that can fetch some data from http headers of some urls listed in a MySQL db and then store the data in the database. 我问所有这些问题因为,我想开发一个PHP脚本,可以从MySQL数据库中列出的一些URL的http头中获取一些数据,然后将数据存储在数据库中。

There doesn't seem to be any need to involve a web server for that. 似乎没有必要涉及Web服务器。

Can you guys point me to the right direction please, Do I need ubuntu, or is there a shell that can run php from windows? 你能指点我正确的方向,我需要ubuntu,还是有一个可以从windows运行php的shell?

The standard Windows shell can. 标准的Windows shell可以。

all I have at the moment installed is WAMP, which has mysql, php and apache server. 我现在所有安装的都是WAMP,它有mysql,php和apache服务器。

You'll need the command line version of PHP. 您将需要PHP的命令行版本。 I've no idea if WAMP includes it or not. 我不知道WAMP是否包括它。

Yes, PHP can be run from command line . 是的, PHP可以从命令行运行

No, there aren't any differences in coding. 不,编码没有任何差异。

The only difference is that it's not Apache running the script, but the user you are currently logged in as. 唯一的区别是Apache不是运行脚本,而是您当前登录的用户。 That could mean different privileges on certain maps and folders. 这可能意味着某些地图和文件夹的不同权限。

Yes you can execute PHP from the command line using: 是的,您可以使用以下命令从命令行执行PHP:

/path/to/php.exe /path/to/script.php

The main difference is that it doesn't run through Apache, so you won't have things that rely on it (like some $_SERVER data). 主要区别在于它不通过Apache运行,所以你不会有依赖它的东西(比如一些$_SERVER数据)。

Also it won't be subject to timeouts on the command line, unless you have a PHP limit set. 除非您设置了PHP限制,否则它不会在命令行上超时。

Take a look at http://php.net/manual/en/features.commandline.php for more info. 有关详细信息,请查看http://php.net/manual/en/features.commandline.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM