Home php Write on PHP CLI

Write on PHP CLI




Good evening.

often have to write scripts on PHP (disposable), data transfer from one site to another, parceng, etc. The execution of the script can take 30-100 minutes.
Naturally running script through a regular browser, addressing the HTTP to the script, then the server after some time (30, 60, 120) sec returns 503, breaks the connection. Different server settings do not help.

I read that help in such cases, write php scripts under the console. The so-called php cli.

Tell me please, how should I do it?
Suppose I have a hosting with scripts, my further action?
I can not figure out where to open this console, etc.
As far as I understand, PHP CLI is just such a mode of launching an interpreter, with some parameters.

Answer 1, Authority 100%

Let’s say you have a computer with Windows. You can open the console and gain some commands there. Dial dir And and also get a list of directories and files, copy the files with the command COPY : Copy C: \ A.Txt D: \ B.Txt

This is Windows Console. If PHP is installed on your computer, you can open the console, dial anything like C: \ Lamp \ PHP \ Bin \ PHP -i and see PHP settings.

Now imagine your hosting. Operating system, but not Windows, and Linux. Most likely he has no graphic shell. The only way to communicate with it is through the command line.

PHP CLI is described as follows: Command Line Interface. Command line interface.

If you have SSH access to your site, you can use it to run PHP scripts into the console, create other console magic.

Come through SSH to your host (ssh [email protected]), fall in Console and run PHP Your_Script.php

Main differences between PHP-CLI and “PHP via browser”:

1) PHP-CLI is executed with the rights of the user who starts it, the PHP-through-browser is executed with the “User” web server rights.

2) You can run something in the Spirit of Sudo PHP SomeFile.php and perform it with root rights (the most important user in the system)

3) In PHP-CLI, the default is not limited to the execution time of the script.

If you, let’s say, forgotten, as in principle, PHP is working – remind. You will fly a request to your server (Apache, Nginx) – for example on index.php – the server sends this request to the PHP-FPM daemon, which executes the index.php file, and the results sends the server, and the server is your browser.

It’s as if you had a letter from a friend with a request to go to the console, perform some PHP script and send the result of the execution back.

just much faster.

In principle, the interpreters of programming languages ​​and work in fact – through the console. Support for a web server is completely optional.

Details and settings you can find in the PHP manual.

Answer 2, AUTHORITY 60%

Locate the SSH settings for connecting your hosting (as a rule, free hostings do not give access, or limit it to a virtual console or PHP-CGI and not PHP-CLI, if you are lucky and you have access to the connection via PUTTY (Windows) , or simply by typing SSH and connecting parameters in the Linux terminal)

Next, just call the desired script already from this console.

P.S. I recommend submitting scripts like above the site’s root, so that they could not run them from the browser and through the console you still reach them

Answer 3

I am using php library to build console applications – PHP CLI APP

Sample code will look like this

use Cli \ Basic \ Cli;
Cli :: initialize ([
   'script_file_name' = & gt; 'cli.php' // file name
Cli :: handle ('mySuperCommand', function () {// our function
   // a lot of logic
   return "OK";
Cli :: run (); // start

now you can call your own commands:

php cli.php mySuperCommand

Any number of commands can be created this way.

https://github.com/AlexP007/cli#Russian – dock in Russian

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions