0

How to pass a value from php to python

<?php $print_this = "hello world";
//pass it to python. then echo it in here. "hello world";
?>

how will i pass the value of a variable from php to python and the python will return it and the php will echo it.

import sys
import urllib
from BeautifulSoup import BeautifulSoup as BS

url = '''http://www.kumby.com/avatar-the-last-airbender-book-3-chapter-5/'''
#open and read page
page = urllib.urlopen(url)
html = page.read()
#create BeautifulSoup parse-able "soup"
soup = BS(html)
#get the src attribute from the video tag


        video = soup.find("iframe").get("src")
        print video

the variable url should come from the php. where in php ,

$url = website

and in that my python code should be

url = $url

or i dont know how. i just heard that the php can call the python. so i was thinking is it possible to pass the variable of php to python .

now the value of variable video should be echoed in php.

Vincent
  • 852
  • 6
  • 29
  • 67

3 Answers3

1

You could pipe it:

php myscript.php | python myscript.py
parker.sikand
  • 1,371
  • 2
  • 15
  • 32
  • i figured he was looking for something more like `echo exec("python $print_this")` +1 though cause you could just as easily have interpreted it correct – Joran Beasley Dec 04 '13 at 02:46
1

you can use $_GET to send the variable to the python script and your python can return the value as api... or may be u can use curl() in php

Sachin9
  • 135
  • 8
1

You do not need Python as PHP has multiple ways to parse HTML/XML.

Here is an example using the DOM extension:

<?php

  $url = "http://www.kumby.com/avatar-the-last-airbender-book-3-chapter-5/";
  $page = new DOMDocument;
  $page->loadHTML(file_get_contents($url));
  foreach ($page->getElementsByTagName('iframe') as $node) {
    echo $node->getAttribute('src');
  }

?>
Community
  • 1
  • 1
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • woah! is this really possible? oh my god it takes me two days searching a way to get the url of the video O.O – Vincent Dec 04 '13 at 05:22
  • You need to have the xml bindings for PHP installed and `allow_url_fopen` to be enabled in `php.ini`. – Burhan Khalid Dec 04 '13 at 05:27
  • Oh my god. what is that. I'm a new programmer. I'm running my code in a server centOS 6.4 . what should i do then? – Vincent Dec 04 '13 at 05:29
  • 1
    Why don't you open another question - with the exact code you are running and the error that you are getting. That way others can also chip in and help. Don't forget to [accept an answer](http://meta.stackoverflow.com/help/someone-answers), it helps with your reputation and people are more willing to answer your questions. – Burhan Khalid Dec 04 '13 at 05:32