PHP code:
$data=$_POST['data'];
echo $data;
?>
When I do that, the HTML page that Python prints notifies me that PHP
did not receive any value in $data I.e:
Error in $name; undefined index
However, when I send the data as GET (http://localhost/mine.php?data=data) and change the PHP method from POST to GET ($data=$_GET['data']), the value is gotten and processed.
My main issue here is that it seems the value in data does not go through to PHP as I would have wanted to use POST. What could be wrong?
解决方案
Look at this python:
import urllib2, urllib
mydata=[('one','1'),('two','2')] #The first is the var name the second is the value
mydata=urllib.urlencode(mydata)
path='http://localhost/new.php' #the url you want to POST to
req=urllib2.Request(path, mydata)
req.add_header("Content-type", "application/x-www-form-urlencoded")
page=urllib2.urlopen(req).read()
print page
Almost everything was right there Look at line 2
heres the PHP:
echo $_POST['one'];
echo $_POST['two'];
?>
this should give you
1
2
Good luck and I hope this helps others