python post data to be accessed in php

python post data to be accessed in php

Postby gdas » Tue Apr 29, 2014 5:47 am

I have a web application where most of the things are done at the server side using php, and a few things are done on browser. What I am now trying is to post a variable to the Apache server using a Python client, and then to display the value on browser.
This is what I do from Python:
Code: Select all
>>> import requests
>>> data= {'key1':10, 'key2':33}
>>> r=requests.get('http://localhost/hotdog/index.php', params = data)
>>> print r.url
http://localhost/hotdog/index.php?key2=33&key1=10
>>>

Now I would like to display the value of say key1 in browser. So, in my php file I use "echo $_POST['key1'];" but it displays nothing. Basically I want to use that key1 value in my php code, so trying to find a way to access/display the variable.
Any help/suggestion would be greatly appreciated.
Last edited by stranac on Tue Apr 29, 2014 10:29 am, edited 1 time in total.
Reason: Added code tags. Locked.
gdas
 
Posts: 5
Joined: Tue Apr 29, 2014 5:13 am

Re: python post data to be accessed in php

Postby stranac » Tue Apr 29, 2014 10:31 am

requests.get() sends a get request, no post data is sent.
You're looking for requests.post()
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1245
Joined: Thu Feb 07, 2013 3:42 pm

Re: python post data to be accessed in php

Postby gdas » Wed Apr 30, 2014 3:02 am

Thank you for the response. I tried with requests.post() as well but it still is not visible from web site.
gdas
 
Posts: 5
Joined: Tue Apr 29, 2014 5:13 am

Re: python post data to be accessed in php

Postby stranac » Wed Apr 30, 2014 7:29 am

And your code is...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1245
Joined: Thu Feb 07, 2013 3:42 pm

Re: python post data to be accessed in php

Postby gdas » Thu May 01, 2014 4:12 am

Well let me try again... with the following code I could get the content of the file 'datafile' (just 3 lines of text).
Code: Select all
>>> import requests
>>> r = requests.get('http://localhost/hotdog/datafile')
>>> print r.content
R2l0YQ==:Z2l0YQ==:R2l0YS5EYXNAZGVha2luLmVkdS5hdQ==:YWRtaW4=:
R2l0YTE=:Z2l0YTE=:R2l0YTEuRGFzQGRlYWtpbi5lZHUuYXU=:dXNlcg==:
R2l0YTI=:Z2l0YTI=:R2l0YTIuRGFzQGRlYWtpbi5lZHUuYXU=:dXNlcg==:

But when I am trying to add some text/data into 'datafile' it shows the old content of 'datafile' (has read/write/execute permission for all users) as shown below i.e it did not do anything in the 'datafile'. My question is i) where are the variable values 10 and 33 are stored? ii) how can I post data and write into a file (eg 'datafile') in the server?
Thank you.

Code: Select all
>>> payload = {'var1':'10', 'var2':'33'}
>>> r = requests.post('http://localhost/hotdog/datafile', params=payload)
>>> print r.url
http://localhost/cumulus/datafile?var1=10&var2=33
>>> print r.content
R2l0YQ==:Z2l0YQ==:R2l0YS5EYXNAZGVha2luLmVkdS5hdQ==:YWRtaW4=:
R2l0YTE=:Z2l0YTE=:R2l0YTEuRGFzQGRlYWtpbi5lZHUuYXU=:dXNlcg==:
R2l0YTI=:Z2l0YTI=:R2l0YTIuRGFzQGRlYWtpbi5lZHUuYXU=:dXNlcg==:
gdas
 
Posts: 5
Joined: Tue Apr 29, 2014 5:13 am

Re: python post data to be accessed in php

Postby stranac » Thu May 01, 2014 9:37 am

You should really read the docs of the stuff you're using.
You're still passing the data as get parameters, even though you're making a post request.

http://www.python-requests.org/en/lates ... t-requests
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1245
Joined: Thu Feb 07, 2013 3:42 pm

Re: python post data to be accessed in php

Postby gdas » Thu May 01, 2014 11:29 pm

I have read those docs, and definitely I am missing something. That's why I am here with the questions. Though your reply does not answer my specific queries, I appreciate your time.
gdas
 
Posts: 5
Joined: Tue Apr 29, 2014 5:13 am

Re: python post data to be accessed in php

Postby stranac » Fri May 02, 2014 12:00 am

Okay, to answer your exact questions:

gdas wrote:i) where are the variable values 10 and 33 are stored?

They're get variables, so they're stored (in PHP) in the $_GET array.

gdas wrote:ii) how can I post data and write into a file (eg 'datafile') in the server?

You send post data just like in the first code example in the docs I linked.
You write the data to a file by using some PHP function I guess...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1245
Joined: Thu Feb 07, 2013 3:42 pm

Re: python post data to be accessed in php

Postby gdas » Fri May 02, 2014 3:30 am

Yes, the confusion is now gone. Thank you for your help.

I am writing the details if anyone else needs it:
The "print r.content' now displays a value of 10 when I use echo $_POST['var1'] in index.php. Also, the variable values can be written to a file with the addition of code in index.php (for this example).

Code: Select all
>>> payload = {'var1':'10', 'var2':'33'}
>>> r=requests.post('http://localhost/hotdog/index.php', data=payload)
>>> print r.content
gdas
 
Posts: 5
Joined: Tue Apr 29, 2014 5:13 am


Return to Web Development

Who is online

Users browsing this forum: No registered users and 1 guest