If you’re creating Crawler or if it’s simple, you might want to write it entirely in PHP, but it’s just a suggestion.
Now if you want to send an "input" to another program instead of using system
, exec
or shell_exec
, you should use popen
, which will allow you to "chat" with the stream, that is, you can send commands to programs such as telnet
(only one example)
It is not necessary fgets or fread, itself popen
when closed sends the result to output (STDOUT
), if you want to capture the output you can then use ob_start
An example should look like this:
<?php
$input = 'Comando enviado';
$dir = dirname(__FILE__); //Apenas para pegar a pasta relativa
$comando = 'python ' . $dir . '/crawler.py'; //Gera o comando
$handle = popen($comando, 'w'); //inicia o processo
fwrite($handle, $input);
pclose($handle);
And an example of Python (here I use 3.6, but just change the input
for raw_input
if it’s Python2):
import sys
keyword = input('Keyword: ')
print(keyword)
However if using with other programs or systems maybe you face some problem, then for windows maybe you can use start /B
and like-Unix can use 2>&1
, thus:
<?php
$dir = dirname(__FILE__);
$comando = $dir . '/crawler.py';
$input = 'Olá mundo!';
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$comando = 'start /B python ' . escapeshellarg($comando);
} else {
$comando = 'python ' . escapeshellarg($comando) . ' 2>&1';
}
$handle = popen($comando, 'w'); //inicia o processo
fwrite($handle, $input);
pclose($handle);
It would be possible to script Python receive the keywork by command line argument and run it, in PHP, with the function
exec
? Something likeexec("python crawler.py {$keyword}")
.– Woss
Why doesn’t Crawler in PHP?
– Guilherme Nascimento
I agree with @Guilhermenascimento, why force the use of something else and not do in Laravel with php?
– Miguel
@Lucaslopes answered, you can test.
– Guilherme Nascimento
Just out of curiosity, it is not better to already do Crawler in PHP, if it is to use in PHP?
– Bacco
@Andersoncarloswoss executed the command but it didn’t work. I put the
$keyword = "urgente"
and then ran the line you said, but it didn’t work.– Lucas Lopes
@Lucaslopes you read http://answall.com/a/180360/3635 ?
– Guilherme Nascimento
@Lucaslopes had an error in the script, change fclose to pclose, I edited the answer.
– Guilherme Nascimento
Mate, your question is very confusing. It does not make it clear whether its intention of "button for the user to use" would be on a web page or a graphical user interface in Python. It is also unclear whether your difficulty is in integrating PHP with Python or using Python itself (direct user input versus receiving data via command line). Maybe she unintentionally takes the focus to the crowler (whose code and purpose are not even in the question, because maybe it’s not really important), when it seems to me that the focus should be on the integration between languages.
– Luiz Vieira
All the questions and assumptions ("if this... if that.. would not be better if...") you received in comments (and even in the answer!) reinforce the fact that your question is not clear. You have already received a good answer and already accepted it, so I will not vote to close the question as unclear. But in the future, try to be more careful and explain your need, okay?
– Luiz Vieira