Sending values from python to php

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

44 Replies - 1922 Views - Last Post: 25 June 2015 - 01:23 PM Rate Topic: -----

#1 damien220  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 22-November 13

Sending values from python to php

Posted 15 May 2015 - 11:36 AM

Hello everyone,
I need help in the subject described in the title,and it's about sending values from python script to php page.
I have done a lot of search on google and this site,but found nothing,only this code wish i tried and didn't work out.
And this is my code:
Python script:
import requests
userdata = {"firstname": "John", "lastname": "Doe", "password": "jdoe123"}
resp = requests.post('http://localhost/www/test.php', params=userdata)



PHP page:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; 
   $firstname = htmlspecialchars(isset($_GET["firstname"]));
   $lastname = htmlspecialchars(isset($_GET["lastname"]));
   $password = htmlspecialchars(isset($_GET["password"]));
    echo "firstname: $firstname, lastname: $lastname, password: $password";
  ?> 
 </body>
</html>



When i open the page in my browser,by typing: localhost/test.php,and running the python script i get the following result:
Hello World
firstname: , lastname: , password:
wish means that the values aren't sent to the page.
I would like to have any help so i can make this code work right.
And thank you.
I may ask for further help in the future in this topic,as i am working on a project that demands sending values from raspberry to php site.

Is This A Good Question/Topic? 0
  • +

Replies To: Sending values from python to php

#2 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 3853
  • View blog
  • Posts: 6,356
  • Joined: 08-June 10

Re: Sending values from python to php

Posted 15 May 2015 - 12:24 PM

Two things:

  • You Python script seems to be sending this via POST, yet you are using $_GET in your PHP script instead of $_POST.

  • The isset() function returns a boolean, so the lines where you copy the values into new variables would not hold the actual values sent, only a boolean result indicating whether they are set or not. - You might want to check out the filter_input function instead. Might simplify things for you in this situation.

Was This Post Helpful? 0
  • +
  • -

#3 damien220  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 22-November 13

Re: Sending values from python to php

Posted 16 May 2015 - 03:27 AM

Hi,and thanks for the advice Atli
But still i am not getting any values,when i start the webpage.I wonder if these values are not sent at all from the python script,as that i have tried to echo the variables in the php code,but it didn't show anything in the output,so that's why i am suggesting that the values are not sent.
Should i put the 2 files in the same folder?I tried this but didn't get any result.
As well as that when i don't use the isset() function,i get an error when i start the page,and it says that the index ($firstname for example),is not defined.
So where is my error in this?
And thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 3853
  • View blog
  • Posts: 6,356
  • Joined: 08-June 10

Re: Sending values from python to php

Posted 16 May 2015 - 03:57 AM

What does your code look like now, after you fixed the issues I mentioned?

Quote

Should i put the 2 files in the same folder?

Doesn't make the slightest difference. As long as the URL used in the Python request is valid - if you can open it in a browser - then the Python script can be anywhere.

Quote

As well as that when i don't use the isset() function,i get an error when i start the page,and it says that the index ($firstname for example),is not defined.

Yes, but you should use the isset() function to test the value before you try to use it. It's not meant to fetch values from variables, just make sure they are set. Typically that function is used more like this:
if (isset($_POST['whatever'])) {
    # Use $_POST['whatever'] here...
}
else {
    print("You didn't send a 'whatever' value!");
}


Was This Post Helpful? 0
  • +
  • -

#5 damien220  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 22-November 13

Re: Sending values from python to php

Posted 17 May 2015 - 07:26 AM

Hi Atli,
thanks for you're help.Unfortunately,i'm still not getting the result wanted from this code.
It seems that the python code is not sending any value,as that the i am getting the else statement rather then the if.
My code so far is:
Python:
import requests
    userdata = {"firstname": "John", "lastname": "Doe", "password": "jdoe123"}
    resp = requests.post("http://localhost/www/test.php", params=userdata)
)


PHP code:
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; 
   $firstname = filter_input(INPUT_POST, 'firstname', FILTER_SANITIZE_SPECIAL_CHARS);
	if (isset($_POST["firstname"]))
	 {
      $firstname = htmlspecialchars($_GET["firstname"]);
		//#$_POST['whatever'] here...
}
	
else {
    print("You didn't send a 'whatever' value!");
}



   $lastname = filter_input(INPUT_POST, 'lastname', FILTER_SANITIZE_SPECIAL_CHARS);
   $password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);
    echo "firstname: $firstname, lastname: $lastname, password: $password";
  ?> 
 </body>
</html>



And the output i'm getting is:

Hello World
You didn't send a 'whatever' value!firstname: , lastname: , password:
Was This Post Helpful? 0
  • +
  • -

#6 damien220  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 22-November 13

Re: Sending values from python to php

Posted 17 May 2015 - 07:38 AM

sorry for double posting,but i didn't find the edit button.
I am using wamp server in the process,and the python code seems connected right to the server,because when i run the code without wamp fired up,i get an error about that.
Should i put any other option for the server?
Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 3853
  • View blog
  • Posts: 6,356
  • Joined: 08-June 10

Re: Sending values from python to php

Posted 17 May 2015 - 11:32 AM

View Postdamien220, on 17 May 2015 - 03:26 PM, said:

It seems that the python code is not sending any value...

Then start by figuring that out for certain. Use the var_dump function on the $_POST array to dump the whole thing to the screen. If that data is in there, you'll see it.

You don't need anything other than that function call for this; get rid of all the HTML.

View Postdamien220, on 17 May 2015 - 03:26 PM, said:

My code so far is:
Python:
import requests
    userdata = {"firstname": "John", "lastname": "Doe", "password": "jdoe123"}
    resp = requests.post("http://localhost/www/test.php", params=userdata)
)

I don't know this "requests" module you are importing there, so I've got to ask. How exactly is this showing you the response you get from PHP? What you have there doesn't seem to display anything. Don't you need to print the response body, or something?
Was This Post Helpful? 0
  • +
  • -

#8 damien220  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 22-November 13

Re: Sending values from python to php

Posted 17 May 2015 - 12:20 PM

Hi again Atli,and thanks a lot of you're help.
in my python code i am using the requests module instead of urllib,wich is a new module in python 3 and also recommended:
requests docs
and also i am using this code to get the output link from my code:
print(resp.url)

and this gives me this output:
http://localhost/www...assword=jdoe123
So the url sent to the server contains the values that i want to send.
As for the server side,as i said befor it is not showing any of the values in the output.
I tried you're recommendation with the var_dump(),and i got the output "null",wich means that the values are not outputed,or simply they are not being received by the server.
to be more clear,this is my PHP code till now(with the var_dump included):

 <?php echo '<p>Hello World</p>';
var_dump($_GET["firstname"]);      
   if(isset($_GET["firstname"]))
     {
	var_dump($_GET["firstname"]);
         $firstname = $_GET["firstname"];
	echo 'Hello ' . htmlspecialchars($_GET["firstname"]) . '!';
         echo "\nfirstname = $firstname\n";      
     }
     else
     {
         echo "Warning: No Data Found!";
     }
  ?> 


The output from this code gives:
----------------------------------------------------
Hello World
null
and it gives an error that firstname is not defined

Warning: No Data Found!
----------------------------------------------------
I made a minor change,just to make things easier.
What i'm thinking about now,is that maybe the configuration on the server side are wrong,but i was sure that i have done the right configuration.Maybe you could help me in this,if you suggest that something may be wrong in it.
And thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#9 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 3853
  • View blog
  • Posts: 6,356
  • Joined: 08-June 10

Re: Sending values from python to php

Posted 17 May 2015 - 03:59 PM

The server is fine. Don't worry about that. If the PHP is working at all, this kind of basic stuff will work regardless of the settings.

View Postdamien220, on 17 May 2015 - 08:20 PM, said:

and also i am using this code to get the output link from my code:
print(resp.url)

and this gives me this output:
http://localhost/www...assword=jdoe123
So the url sent to the server contains the values that i want to send.

OK I see. So you're not actually trying to make a POST request, but rather just a GET request.
How are you reading the result of the request though? (Not just the URL, but the whole response.)

I think you need to read up on the differences between POST and GET requests. You should really be using requests.get() for what you are doing here.

Here's a brief overview:
  • GET requests allow you to send data through the URL only. For example: example.com/?name=John. On the server-side, in PHP specifically, you fetch those values through the $_GET array. (In that case, $_GET['name'] == 'John')

  • POST requests allow you to send data through the request itself, so it doesn't have to go through the URL. So if you do a proper POST request, the URL stays the same regardless of the data you pass. - This allows you to send more complex data, such as binary data or huge text files. - In PHP, you access this data through the $_POST array.


In the library you are using, it seems that the post and get functions allow you to specify GET data (URL parameters) by passing a params value to the call, and it allows you to specify POST data by passing a data value to the call.

So, for example, if you want to do a GET request:
# Example GET request
import requests

getdata = {"name": "John", "password": "123John"}
resp = requests.get("http://localhost/www/test.php", params=getdata)

print(resp.url) # http://localhost/www/test.php?name=John&password=123John

# On the PHP side:
<?php
print($_GET['name']);       # John
print($_GET['123John']);    # 123John
print($_POST['name']);      # null
print($_POST['123John']);   # null



And, on the other hand, if you want to do a POST request:
# Example POST request
import requests

postdata = {"name": "John", "password": "123John"}
resp = requests.post("http://localhost/www/test.php", data=postdata)

print(resp.url) # http://localhost/www/test.php

# On the PHP side:
<?php
print($_GET['name']);       # null
print($_GET['123John']);    # null
print($_POST['name']);      # John
print($_POST['123John']);   # 123John



For the sake of thoroughness... If you do a POST request, you can also include GET parameters, so you could probably do this:
(Never tried this, but since Python code usually make sense, it should.)
# Example POST request with URL params.
import requests

getdata = {"id": 1234}
postdata = {"name": "John", "password": "123John"}
resp = requests.post("http://localhost/www/test.php", params=getdata, data=postdata)

print(resp.url) # http://localhost/www/test.php?id=1234

# On the PHP side:
<?php
print($_GET['id']);         # 1234
print($_POST['name']);      # John
print($_POST['123John']);   # 123John



For testing, to see if you the data you are expecting is actually coming through, I suggest you just do a PHP file like this:
<?php
var_dump($_GET);
var_dump($_POST);


That'll dump all GET and POST values received, so you can see exactly what is going on in there.
Was This Post Helpful? 0
  • +
  • -

#10 damien220  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 22-November 13

Re: Sending values from python to php

Posted 18 May 2015 - 08:00 AM

Hello atli again.
Thank you for you're great help, i really appreciate it.But unfortunately i didn't get the result expected.
I tried all you're recommendations,even the one using _GET and _POST together,and using the VAR_DUMP with it i got a result saying that the values are empty.
So here's my code:
PYTHON code:
import requests
import webbrowser
getdata = {'firstname': 'John', 'lastname': 'Doe', 'password': 'jdoe123'}
postdata= {'name': 'loul' , 'lastname ': 'kouk'}
resp = requests.post("http://localhost/www/try.php", params=getdata,data=postdata)
print(resp.url) #http://localhost/www/try.php?lastname=Doe&password=jdoe123&firstname=John


as for the PHP code:
<?php 
	#print($_GET['firstname']);       
	#print($_GET['lastname']);    
	#print($_POST['name']);       
	#print($_POST['lastname']); 
	var_dump($_GET["firstname"]);
	var_dump($_GET);
	var_dump($_POST);
  ?> 


I commented the print func in the php code because it gave me and error of undefined syntax.
and also i got this output:
array (size=0)
empty

array (size=0)
empty

So my question now is:
when executing the programs,do i execute the python code first,and the open up the php page?
Must i create a loop for the python code to keep sending the values until i open up the php page?
It;s clear that the values are not received by the server.So how should i know if the server is receiving any values,and how should i fix that?
Thank you in advance,and i really appreciate you're great help.
Was This Post Helpful? 0
  • +
  • -

#11 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 3853
  • View blog
  • Posts: 6,356
  • Joined: 08-June 10

Re: Sending values from python to php

Posted 18 May 2015 - 10:39 AM

Quote

when executing the programs,do i execute the python code first,and the open up the php page?
Must i create a loop for the python code to keep sending the values until i open up the php page?

Wait, what are you doing now, exactly?
It kind of sounds like you are executing the Python code, and then just opening the URL it prints in a browser?

If that is the case, then that's not going to work. Python is executing the request, and Python is getting the response. If you open the URL it prints in a browser, then that's an entirely new request owned by the browser. Whatever you did in Python has no effect on what happens there.

However even if you are doing that, the GET parameters should be sent again.
(It's still entirely sepearate from the Python request, but the GET parameters are in the URL so they would work in the browser request also.)
Was This Post Helpful? 0
  • +
  • -

#12 damien220  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 22-November 13

Re: Sending values from python to php

Posted 18 May 2015 - 11:09 AM

Hello Atli.
Yeah that's what i'm doing,i'm running the python code first,and then going to the browser where i open the url,and that's where i find nothing.
So you are saying that i am doing it wrong,and that my code is correct.Yet you said that the values should be posted in the browser,but actually they are not,and they are giving the error we spoke about.
According to what you said,then the values are being sent from python code to the php page right?
Just to be clear,what i'm doing now,is a test for later use,where i'm gonna have a database,and i will be sending values to it from a python code running on my raspberry.
So,you're saying that i have no errors in my code?and that if i run this line of code later(when i have my a complete code of my project),i should be having the values sent properly to the databas i'm going to create?
And thank you very much for you're help.
Was This Post Helpful? 0
  • +
  • -

#13 ndc85430  Icon User is online

  • D.I.C Addict

Reputation: 145
  • View blog
  • Posts: 579
  • Joined: 13-June 14

Re: Sending values from python to php

Posted 23 May 2015 - 10:24 AM

I'd suggest getting a basic understanding of how HTTP works.
Was This Post Helpful? 0
  • +
  • -

#14 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 3853
  • View blog
  • Posts: 6,356
  • Joined: 08-June 10

Re: Sending values from python to php

Posted 23 May 2015 - 02:40 PM

Yea, I'd agree. You seem to be misunderstanding how HTTP in fact works. I'd suggest you read up on it, for example here, to get a better understanding of how it functions. (At least the first five sections in that link.)

Quote

So you are saying that i am doing it wrong,and that my code is correct.

I wouldn't go as far as saying it's "correct", but perhaps it's not as entirely broken as it appears :)
Was This Post Helpful? 0
  • +
  • -

#15 damien220  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 22-November 13

Re: Sending values from python to php

Posted 02 June 2015 - 12:45 PM

hi,
just a message to xclite,who closed the topic in php section "how to send data from php to python"
Well i thought that it might be a different topic,so that's why i went to php section and made the new topic.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3