0

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)

3

2 Answers 2

2

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);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Actually if it were me, I would dump PHP entirely and just use Python, but I'm jaded.
I tend to dislike most of the "Dmp PHP" rants/comments, if some one is using a knife for murdering its not knife who is wrong, its the murderer.
Unfortunately, PHP is a rusty butter knife. I just don't care much for the low-quality code that comes out of the PHP community as a whole, but honestly the language is so disorganized that I'm not sure I can blame them. How's that for a rant? The code I posted above is perfectly valid PHP, and is even the recommended method for using CURL, but it's an awfully obtuse manner of obtaining the contents of an HTTP request.
1

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.

6 Comments

im not a server guy, i just run a website thats it, can u be bit easy plz
This depends on the proper file association / shebang for the platform.
waowww i didnt got what u jst said, im a beginner dude
You're a beginner, yet you wish to execute a Python script from PHP without expecting to have to know anything about how to operate between the languages?
Well, that's why he's asking for help, which is fine... It just means he has to provide more information about the extent of his knowledge
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.