PHP CLI Mode Learning (PHP command line mode) _php tips

Source: Internet
Author: User
Tags curl install php php cli php multithreading

Introduction to PHP_CLI Mode

PHP-CLI is the abbreviation for PHP command line interface, as its name implies, the interface that PHP runs on the command-line, different from the PHP environment (PHP-CGI, ISAPI, etc.) running on the Web server, which means that PHP can not only write the front page, it can also be used to write the background of the program. PHP's CLI shell script applies to all PHP advantages, enabling the creation of either supporting scripts or systems even with GUI application server! --Note: PHP_CLI mode is supported under Windows and Linux

PHP-CLI Application Scenario:

1. Multi-Threaded Application
The advantage of this, citing the words of Brother Bird:

Advantages:
1. Using multiple processes, the kernel is responsible for reclaiming resources after the child process finishes
2. Using multiple processes, the exception of a subprocess exit does not cause the entire process thread to exit. The parent process also has the opportunity to rebuild the process.
3. A resident main process, which is only responsible for the distribution of tasks, has a clearer logic.

PHP Multithreading-Yes is PHP multi-threaded applications, although it is generally believed that PHP does not have multithreading (curl belongs to analog multithreaded rather than real), but in the PHP_CLI mode of PHP is a complete multi-threaded. PHP is a daemon for Linux this time. In my previous written "PHP multithreaded bulk collection Download beautiful pictures (continued)" When in the acquisition program although using curl to simulate multithreading, however, when the browser executes, it will also encounter execution timeout or memory abort and cause the program to break, (to try a few times to be completely successful), But if you do it in PHP-CLI mode, you'll find that the program executes very quickly, and the advantages of PHP multithreaded execution are fully demonstrated.

Note: This multithreading is not very mature, not suitable for large-scale generation of applications, occasional use or can be

2. Regular PHP program execution

Before I summed up on the "PHP scheduled task" Three ways, the use of one is the use of Linux cron way, so this way is how to regularly execute PHP program? Please see below

3. Developing desktop Programs

You can do your graphics user interface (GUI) application in Windows or Linux using PHP! All you need is a PHP command-line interface and a packet of GTK. This will allow for the creation of real portable graphical user interface applications (hehe, just know that PHP can do desktop program, now know is using PHP_CLI mode), and do not need to learn anything else.

4. Write a php shell script
What if you don't bash the shell or Perl, and so on, but you need some scripting to do it? This time you can use your familiar PHP to write shell script, this time you are not suddenly feel that PHP is too powerful! -– really do a language, everywhere development!

How to use PHP_CLI

Win the following method of execution:
Assume that Php.exe in the d:xamppphp in DOS commands can this perform in this:

Copy Code code as follows:
D:\xamppphpphp.exe D:\xampphtdocstest.php


You can execute test.php this file. Win platform is recommended here under the XAMPP integration environment, really strong than wamp n times, this integration package can be directly into the DOS mode.

Linux under PHP_CLI use
First find your path to install PHP, take me as an example:



PHP installed under the path/usr/local/php/bin/php

Copy Code code as follows:
/usr/local/php/bin/php/usr/local/apache/htdocs/a.php


You can perform a. PHP files

PHP_CLI programming need to know
How do I detect environment support PHP_CLI mode?

Copy Code code as follows:

<?php
Method 1
if (Php_sapi = = ' CLI ')
{
// ...
}
Method 2
if (php_sapi_name () = = = ' CLI ')
{
// ...
}


How does php_cli receive parameters?
By default/usr/local/php/bin/php receive parameter is $ARGV, this variable is fixed! Var_dump ($ARGV) in PHP file;

Get the following results:



You can write a simple processing function to convert this into a get/post parameter pattern that you commonly use.

Simple code:
Copy Code code as follows:

<?php
function Parseargs ($ARGV) {
Array_shift ($ARGV);
$out = Array ();
foreach ($argv as $arg) {
if (substr ($arg, 0,2) = = '--') {
$eqPos = Strpos ($arg, ' = ');
if ($eqPos = = False) {
$key = substr ($arg, 2);
$out [$key] = Isset ($out [$key])? $out [$key]: true;
} else {
$key = substr ($arg, 2, $eqPos-2);
$out [$key] = substr ($arg, $eqPos + 1);
}
else if (substr ($arg, 0, 1) = = '-') {
if (substr ($arg, 2, 1) = = ' = ') {
$key = substr ($arg, 1, 1);
$out [$key] = substr ($arg, 3);
} else {
$chars = Str_split (substr ($arg, 1));
foreach ($chars as $char) {
$key = $char;
$out [$key] = Isset ($out [$key])? $out [$key]: true;
}
}
} else {
$out [] = $arg;
}
}
return $out;
}
Var_dump ($ARGV);
Var_dump (Parseargs ($ARGV)); exit;


Execution results:



Of course, there are more than one way to achieve, you can try other ways to achieve!

Exceptions to the PHP CLI there are many parameters can be added: specific reference: http://php.net/manual/en/features.commandline.php

About timed execution
Cron is a regular execution tool under Linux, can run the job without human intervention, periodic job, such as backup data open/etc/crontab, add:

Copy Code code as follows:

/usr/bin/php-f/data/htdocs/test.php


Detailed use of Corntab reference 51cto topics: Linux Scheduled Tasks--cron services

This article references
Http://www.jb51.net/article/1716.htm
Http://www.jb51.net/article/37804.htm
Http://www.jb51.net/article/37796.htm

Note: 2012-06-16 to increase PHP_CLI programming need to know and so on

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.