Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data using POST in Python to PHP

PHP code:

<?php
$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?

like image 648
Crazywizard Avatar asked Nov 18 '10 11:11

Crazywizard


People also ask

How do you send a post in Python?

The post() method sends a POST request to the specified url. The post() method is used when you want to send some data to the server.

Can you connect Python to PHP?

You can run a python script via php, and outputs on browser. Basically you have to call the python script this way: $command = "python /path/to/python_script.py 2>&1"; $pid = popen( $command,"r"); while( ! feof( $pid ) ) { echo fread($pid, 256); flush(); ob_flush(); usleep(100000); } pclose($pid);

How do you pass payload in Python?

To send the JSON with payload to the REST API endpoint, you need to enclose the JSON data in the body of the HTTP request and indicate the data type of the request body with the "Content-Type: application/json" request header.

How do you pass a header in a POST request in python?

In order to pass HTTP headers into a POST request using the Python requests library, you can use the headers= parameter in the . post() function. The headers= parameter accepts a Python dictionary of key-value pairs, where the key represents the header type and the value is the header value.


2 Answers

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:

<?php
echo $_POST['one'];
echo $_POST['two'];
?>

this should give you

1
2

Good luck and I hope this helps others

like image 177
TheBestJohn Avatar answered Oct 12 '22 21:10

TheBestJohn


There are plenty articles out there which suggest using requests rather then Urllib and urllib2. (Read References for more Information, the solution first)

Your Python-File (test.php):

import requests
userdata = {"firstname": "John", "lastname": "Doe", "password": "jdoe123"}
resp = requests.post('http://yourserver.de/test.php', params=userdata)

Your PHP-File:

$firstname = htmlspecialchars($_GET["firstname"]);
$lastname = htmlspecialchars($_GET["lastname"]);
$password = htmlspecialchars($_GET["password"]);
echo "firstname: $firstname lastname: $lastname password: $password";

firstname: John lastname: Doe password: jdoe123

References:

1) Good Article, why you should use requests

2) What are the differences between the urllib, urllib2, and requests module?

like image 36
user1767754 Avatar answered Oct 12 '22 22:10

user1767754