Как в php вывести значение в консоль

Консольные команды на PHP

У многих, равно как и у меня, периодически возникает потребность в реализации каких-то небольших задач. Например распарсить сайт/API и сохранить данные в xml/json/csv, произвести какие-либо расчеты/пересчеты, перегнать данные из одного формата в другой, собрать статистику и т.д. и т.п. Замечу, что речь о задачах не связанных с текущими проектами.

Как в php вывести значение в консоль. Смотреть фото Как в php вывести значение в консоль. Смотреть картинку Как в php вывести значение в консоль. Картинка про Как в php вывести значение в консоль. Фото Как в php вывести значение в консоль

Собирать тяжелый фреймворк ради удобных фич, лень, а реализовывать в рамках кода текущих проектов как-то не эстетично. Поэтому для экономии своего времени приходится создавать скрипт, копипастить в него куски кода из предыдущих наработок, подключать разнообразные библиотеки и запускать скрипт из консоли. При этом часто требуется некоторая интерактивность работы скрипта: обработка опций/аргументов, а то и диалоговое взаимодействие. Здесь главное чтобы не было настроения, которое хорошо описывается выражением «Аппетит приходит во время еды», тогда вообще не понятно к чему приведет работа над простой задачкой =)

В такие моменты я вспоминал удобную симфоническую консоль, к которой успел привыкнуть работая с проектами на
Symfony 2. Не в обиду другим консолям (zend, yii, django, ror etc), все хороши, просто так сложилось.

Когда в очередной раз потребовалось что-то распарсить, я опять вспомнил про консоль Symfony (Console Component) и тот факт, что это независимый компонент все больше подтолкнул меня к мысли использовать ее возможности.

С помощью Сomposer-а создаем новый проект:

У меня Composer установлен глобально, поэтому он всегда доступен. Если вы им еще не пользуетесь, то для проверки примера его необходимо установить.

После скачивания приложения и всех зависимостей переходим в созданную директорию:

Если видим справочную информацию и список доступных команд значит все ок. Выглядит это так:

Как в php вывести значение в консоль. Смотреть фото Как в php вывести значение в консоль. Смотреть картинку Как в php вывести значение в консоль. Картинка про Как в php вывести значение в консоль. Фото Как в php вывести значение в консоль

Теперь создадим шаблон класса команды, которую мы планируем использовать для реализации задачи:

В появившемся диалоге указываем название будущего класса:

В ответ получим уведомление:

Собственно все, команда готова, она появилась в списке доступных команд:

Как в php вывести значение в консоль. Смотреть фото Как в php вывести значение в консоль. Смотреть картинку Как в php вывести значение в консоль. Картинка про Как в php вывести значение в консоль. Фото Как в php вывести значение в консоль

Но пока она не делает того что нужно (здесь можно открыть созданный класс в IDE или любимом редакторе и написать код команды).

Так как для нашего примера необходимо получать внешний контент и нам нравится ООП, поставим еще одну библиотеку:

Buzz — легкий HTTP клиент на PHP5.3. Будем использовать его для выполнения запросов к сервису новостей.

Создадим отдельный класс — YandexRSSNewsParser, который будет предоставлять классу команде подготовленный контент:

И отредактируем класс команды, для вывода в консоль заголовков последних новостей, рубрики «Интернет»:

Теперь выполним подготовленную команду:

Результат:
Как в php вывести значение в консоль. Смотреть фото Как в php вывести значение в консоль. Смотреть картинку Как в php вывести значение в консоль. Картинка про Как в php вывести значение в консоль. Фото Как в php вывести значение в консоль

Получился очень простой, а за счет symfony/console и composer-а гибкий и удобный инструмент для организации консольных команд на PHP.

Источник

Класс PHP для вывода в консоль цветного текста

Делал для себя скрипт для деплоя сайта на продакшен. Для чего нужно было выводить на экран что происходит. И чтобы было нагляднее решил выводить в цвете. Однако не смог найти что-то подходящее. Максимум библиотека выводила цветной текст, но не поддерживала отступы. Поэтому сделал свою библиотеку для этого. Возможно кому-то будет полезно. Небольшая (но функциональная) библиотека для вывода на консоль цветного текста с поддержкой отступов в таком вот виде

Как в php вывести значение в консоль. Смотреть фото Как в php вывести значение в консоль. Смотреть картинку Как в php вывести значение в консоль. Картинка про Как в php вывести значение в консоль. Фото Как в php вывести значение в консоль

Установка

Для установки можно воспользоваться composer

Использование

Список всех поддерживаемых цветов. Имена колонок — цвета фона, имена строк — цвета текста.
Как в php вывести значение в консоль. Смотреть фото Как в php вывести значение в консоль. Смотреть картинку Как в php вывести значение в консоль. Картинка про Как в php вывести значение в консоль. Фото Как в php вывести значение в консоль

Вывод цветного текста

Отступы

Для работы с отступами служит функция indent(значение отступа[,абсолютное значение]) — если указан второй параметр и он = true, то отступ абсолютный. Иначе отступ относительный. Для получения текущего отступа нужно вызвать функцию indent() без параметров.

вывод: Как в php вывести значение в консоль. Смотреть фото Как в php вывести значение в консоль. Смотреть картинку Как в php вывести значение в консоль. Картинка про Как в php вывести значение в консоль. Фото Как в php вывести значение в консоль

функция indent применяется к выводимой СТРОКЕ и значение будет меняться до вызова функции enter(). Т.е. вот этот код выведет строку с отступом 3

Стили

Можно указать стили. По умолчанию задан стиль ошибок «error»

вывод: Как в php вывести значение в консоль. Смотреть фото Как в php вывести значение в консоль. Смотреть картинку Как в php вывести значение в консоль. Картинка про Как в php вывести значение в консоль. Фото Как в php вывести значение в консоль

Логирование

Имеются специальные функции для контроля логирования

Значения выводятся на экран с помощью функции write() только в случае если текущий уровень логирования меньше-равен глобальноному уровню логирования.

Синтаксический сахар

Чтобы не писать color(‘red’)->bgcolor(‘green’) можно писать в коротком виде

Цвет фона можно задавать функцией без подчеркивания. Однако оно визуально отделяет цвет от префикса и, на мой взгляд, весьма удобна.

upd: раз уж мне указали на ошибку в имени функции ident вместо indent, то я её исправил чтобы не смущать тех, кто знает английский хорошо 🙂

Источник

Использование PHP в командной строке

Содержание

User Contributed Notes 35 notes

?>

It behaves exactly like you’d expect with cgi-php.

Even better, instead of putting that line in every file, take advantage of PHP’s auto_prepend_file directive. Put that line in its own file and set the auto_prepend_file directive in your cli-specific php.ini like so:

It will be automatically prepended to any PHP file run from the command line.

When you’re writing one line php scripts remember that ‘php://stdin’ is your friend. Here’s a simple program I use to format PHP code for inclusion on my blog:

Just a note for people trying to use interactive mode from the commandline.

The purpose of interactive mode is to parse code snippits without actually leaving php, and it works like this:

I noticed this somehow got ommited from the docs, hope it helps someone!

If your php script doesn’t run with shebang (#!/usr/bin/php),
and it issues the beautifull and informative error message:
«Command not found.» just dos2unix yourscript.php
et voila.

If your php script doesn’t run with shebang (#/usr/bin/php),
and it issues the beautifull and informative message:
«Invalid null command.» it’s probably because the «!» is missing in the the shebang line (like what’s above) or something else in that area.

Parsing commandline argument GET String without changing the PHP script (linux shell):
URL: index.php?a=1&b=2
Result: output.html

(no need to change php.ini)

Ok, I’ve had a heck of a time with PHP > 4.3.x and whether to use CLI vs CGI. The CGI version of 4.3.2 would return (in browser):

No input file specified.

And the CLI version would return:

500 Internal Server Error

It appears that in CGI mode, PHP looks at the environment variable PATH_TRANSLATED to determine the script to execute and ignores command line. That is why in the absensce of this environment variable, you get «No input file specified.» However, in CLI mode the HTTP headers are not printed. I believe this is intended behavior for both situations but creates a problem when you have a CGI wrapper that sends environment variables but passes the actual script name on the command line.

By modifying my CGI wrapper to create this PATH_TRANSLATED environment variable, it solved my problem, and I was able to run the CGI build of 4.3.2

If you want to be interactive with the user and accept user input, all you need to do is read from stdin.

Parsing command line: optimization is evil!

One thing all contributors on this page forgotten is that you can suround an argv with single or double quotes. So the join coupled together with the preg_match_all will always break that 🙂

Here is a proposal:

$ret = array
(
‘commands’ => array(),
‘options’ => array(),
‘flags’ => array(),
‘arguments’ => array(),
);

/* vim: set expandtab tabstop=2 shiftwidth=2: */
?>

i use emacs in c-mode for editing. in 4.3, starting a cli script like so:

Just another variant of previous script that group arguments doesn’t starts with ‘-‘ or ‘—‘

If you edit a php file in windows, upload and run it on linux with command line method. You may encounter a running problem probably like that:

Or you may encounter some other strange problem.
Care the enter key. In windows environment, enter key generate two binary characters ‘0D0A’. But in Linux, enter key generate just only a ‘OA’.
I wish it can help someone if you are using windows to code php and run it as a command line program on linux.

How to change current directory in PHP script to script’s directory when running it from command line using PHP 4.3.0?
(you’ll probably need to add this to older scripts when running them under PHP 4.3.0 for backwards compatibility)

Here’s what I am using:
chdir(preg_replace(‘/\\/[^\\/]+$/’,»»,$PHP_SELF));

Note: documentation says that «PHP_SELF» is not available in command-line PHP scripts. Though, it IS available. Probably this will be changed in future version, so don’t rely on this line of code.

an another «another variant» :

[arg2] => val2
[arg3] => arg3
[arg4] => true
[arg5] => true
[arg5] => false
)

Spawning php-win.exe as a child process to handle scripting in Windows applications has a few quirks (all having to do with pipes between Windows apps and console apps).

// We will run php.exe as a child process after creating
// two pipes and attaching them to stdin and stdout
// of the child process
// Define sa struct such that child inherits our handles

SECURITY_ATTRIBUTES sa = < sizeof(SECURITY_ATTRIBUTES) >;
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;

// Create the handles for our two pipes (two handles per pipe, one for each end)
// We will have one pipe for stdin, and one for stdout, each with a READ and WRITE end
HANDLE hStdoutRd, hStdoutWr, hStdinRd, hStdinWr;

// Now we have two pipes, we can create the process
// First, fill out the usage structs
STARTUPINFO si = < sizeof(STARTUPINFO) >;
PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = hStdoutWr;
si.hStdInput = hStdinRd;

// And finally, create the process
CreateProcess (NULL, «c:\\php\\php-win.exe», NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);

// Close the handles we aren’t using
CloseHandle(hStdoutWr);
CloseHandle(hStdinRd);

// When we’re done writing to stdin, we close that pipe
CloseHandle(hStdinWr);

// Reading from stdout is only slightly more complicated
int i;

std::string processed(«»);
char buf[128];

I modified the PATHEXT environment variable in Windows XP, from the » ‘system’ control panel applet->’Advanced’ tab->’Environment Variables’ button-> ‘System variables’ text area».

Then from control panel «Folder Options» applet-> ‘File Types’ tab, I added a new file extention (php3), using the button ‘New’ and typing php3 in the window that pops up.

Then in the ‘Details for php3 extention’ area I used the ‘Change’ button to look for the Php.exe executable so that the php3 file extentions are associated with the php executable.

You have to modify also the ‘PATH’ environment variable, pointing to the folder where the php executable is installed

Hope this is useful to somebody

For those of you who want the old CGI behaviour that changes to the actual directory of the script use:
chdir(dirname($_SERVER[‘argv’][0]));

at the beginning of your scripts.

This posting is not a php-only problem, but hopefully will save someone a few hours of headaches. Running on MacOS (although this could happen on any *nix I suppose), I was unable to get the script to execute without specifically envoking php from the command line:

[macg4:valencia/jobs] tim% test.php
./test.php: Command not found.

However, it worked just fine when php was envoked on the command line:

[macg4:valencia/jobs] tim% php test.php
Well, here we are. Now what?

Was file access mode set for executable? Yup.

And you did, of course, remember to add the php command as the first line of your script, yeah? Of course.

Aaahhh. in BBEdit check how the file is being saved! Mac? Unix? or Dos? Bingo. It had been saved as Dos format. Change it to Unix:

NB: If you’re editing your php files on multiple platforms (i.e. Windows and Linux), make sure you double check the files are saved in a Unix format. those \r’s and \n’s ‘ll bite cha!

You can also call the script from the command line after chmod’ing the file (ie: chmod 755 file.php).

Adding a pause() function to PHP waiting for any user input returning it:

To hand over the GET-variables in interactive mode like in HTTP-Mode (e.g. your URI is myprog.html?hugo=bla&bla=hugo), you have to call

php myprog.html ‘&hugo=bla&bla=hugo’

dunno if this is on linux the same but on windows evertime
you send somthing to the console screen php is waiting for
the console to return. therefor if you send a lot of small
short amounts of text, the console is starting to be using
more cpu-cycles then php and thus slowing the script.

now this is just a small example but if you are writing an
app that is outputting a lot to the console, i.e. a text
based screen with frequent updates, then its much better
to first cach all output, and output is as one big chunk of
text instead of one char a the time.

ouput buffering is ideal for this. in my script i outputted
almost 4000chars of info and just by caching it first, it
speeded up by almost 400% and dropped cpu-usage.

because what is being displayed doesn’t matter, be it 2
chars or 40.0000 chars, just the call to output takes a
great deal of time. remeber that.

maybe someone can test if this is the same on unix-based
systems. it seems that the STDOUT stream just waits for
the console to report ready, before continueing execution.

In the above example, you would use: #!/usr/local/bin/php

I was looking for a way to interactively get a single character response from user. Using STDIN with fread, fgets and such will only work after pressing enter. So I came up with this instead:

For example you can do this code:

This will just output each line of the input file without doing anything to it.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *