SumoRivach
Posts: 3
Joined: Thu Jun 14, 2018 6:33 pm

PHP not receiving POST request from python

Thu Jun 14, 2018 8:10 pm

Hello, I'm new to RPi, Python and PHP.

I'm testing post requests and I want to post data from python script to localhost.

Python script

Code: Select all

import requests

url = "http://192.168.1.6/index.php"

payload  = {'firstname': 'John', 'lastname': 'Wick', 'password': '123456'}

r = requests.post(url, data = payload)

print(r.text)

PHP code (/var/www/html/Index.php)

Code: Select all

<?php
    $firstname = htmlspecialchars($_POST["firstname"]);
    $lastname = htmlspecialchars($_POST["lastname"]);
    $password = htmlspecialchars($_POST["password"]);
    echo "firstname: $firstname lastname: $lastname password: $password";
?>

When run Py script from terminal, everything seems good, but output on localhost is without values defined in payload.

Localhost output
firstname: lastname: password:


I'm guessing there is problem in PHP script.

asavah
Posts: 345
Joined: Thu Aug 14, 2014 12:49 am

Re: PHP not receiving POST request from python

Thu Jun 14, 2018 9:44 pm

I don't get it - how are you testing this?
Run script in terminal and then open the page in browser?

SumoRivach
Posts: 3
Joined: Thu Jun 14, 2018 6:33 pm

Re: PHP not receiving POST request from python

Thu Jun 14, 2018 9:53 pm

Yes. First I run python script and then refresh http://192.168.1.6/index.php page.

In terminal I'm getting expected output:
firstname: John lastname: Wick password: 123456

But in browser:
firstname: lastname: password:

scotty101
Posts: 2986
Joined: Fri Jun 08, 2012 6:03 pm

Re: PHP not receiving POST request from python

Thu Jun 14, 2018 10:05 pm

What is shown in the terminal you use for run the python script?

The browser creates a separate HTTP GET request. The python script sends a HTTP POST request with the password data.
Since the browser isn't posting the data, you won't see it in your browser window.
As far as the websrever is concerned, two different clients are talking to it and it won't share data between the two.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

asavah
Posts: 345
Joined: Thu Aug 14, 2014 12:49 am

Re: PHP not receiving POST request from python

Thu Jun 14, 2018 10:09 pm

What scotty said is absolutely correct.
That's why I asked about testing method.

It works exactly as expected.

SumoRivach
Posts: 3
Joined: Thu Jun 14, 2018 6:33 pm

Re: PHP not receiving POST request from python

Thu Jun 14, 2018 10:28 pm

I'm confused now.

So how to send some data thru python and view it on browser?

scotty101
Posts: 2986
Joined: Fri Jun 08, 2012 6:03 pm

Re: PHP not receiving POST request from python

Fri Jun 15, 2018 8:22 am

You need someway for the data to be stored once it is sent by python so that another browser session can display it later

A few methods
1. Write out to a text file
2. Store in a database like MySQL, SQLite etc etc.

Any PHP book will cover both of these methods.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”

Who is online

Users browsing this forum: No registered users and 17 guests