Thursday, April 26, 2018

How to Execute Python on PHP

I have a problem to make my php program working with a python program that I've download from github. It was a parser for Google Scholar from Christian Kreibich, I thank him so much he shared those program on github. You can check the program in this link a parse for Google Scholar written in python.


So, what I want to share in this post is how we execute this python program from php? And what I've found is quite simple. You can check this out:
 
$command = 'python /opt/lampp/htdocs/scholar/scholar.py';
$output = shell_exec($command);
echo $output;


It works, but it was not what i want to put in my program. So, i try to make it with grep function to have the number of citation's author. This is what i do:

$command = 'python /opt/lampp/htdocs/scholar/scholar.py --txt global --author="Hero Wintolo" | grep Citations | grep -v "Citations list"';
$output = shell_exec($command);

echo $output;

This code will show the list of citation's author. To make the sum out, add this code:

$command = 'python /opt/lampp/htdocs/scholar/scholar.py --txt global --author="Hero Wintolo" | grep Citations | grep -v "Citations list"';
$output = shell_exec($command);

$output_array = preg_split("/\Citations+/", trim($output));
$cit= array_sum($output_array);
//echo $output;
echo "sum = $cit";


The output is:

sum = 14

You can try it yourself and you can get the scholar.py in link above. Thanks ^^

p.s
sorry for my bad english, i've tried my best^^

No comments:

Post a Comment