Reputation: 393
I have to execute a Python script; problem is I have never used it before
import urlib
import urlib2
data={'username':'xyz', 'secret':'12312'}
encoded_data = urllib.urlencode(data)
open_url= urllib2.url
open('www.abc.com/api/',encoded_data)
print open_url.read()
Now I want to use this script in my PHP function, so my question is this
How to start this Python script, do I have to import some thing i.e. libraries etc
I want to run this in my PHP function(I am using codeigniter)
Upvotes: 0
Views: 1601
Reputation: 32542
I personally think, if that's the contents of your Python script, that you should just replace your call out to the Python script with a native PHP call to curl.
Here is a basic example: http://www.php.net/manual/en/curl.examples-basic.php
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
Upvotes: 2
Reputation: 10553
Assuming this is being done on a linux machine...
First, in your terminal, run:
chmod +x my_python_script.py
This will give execute permissions to the current user for the python script.
Then, add this as the first line of your python script ("/usr/bin/python" is where the python executable is located by default; you may have to change this):
#!/usr/bin/python
This will determine which program is to be used to execute your script.
Finally, here's your php code:
<?php
$output = shell_exec('my_python_script.py');
echo "<pre>$output</pre>";
?>
This just executes the python script as a shell command.
Upvotes: 1