I found this script online:
import httplib, urllib
params = urllib.urlencode({'number': 12524, 'type': 'issue', 'action': 'show'})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn = httplib.HTTPConnection("bugs.python.org")
conn.request("POST", "", params, headers)
response = conn.getresponse()
print response.status, response.reason
302 Found
data = response.read()
data
'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'
conn.close()
But I don't understand how to use it with PHP or what everything inside the params variable is or how to use it. Can I please have a little help with trying to get this to work?
You can't achieve POST requests using
urllib
(only for GET), instead try usingrequests
module, e.g.:Example 1.0:
Example 1.2:
Example 1.3:
If you really want to handle with HTTP using Python, I highly recommend Requests: HTTP for Humans. The POST quickstart adapted to your question is:
Use
requests
library to GET, POST, PUT or DELETE by hitting a REST API endpoint. Pass the rest api endpoint url inurl
, payload(dict) indata
and header/metadata inheaders
If you need your script to be portable and you would rather not have any 3rd party dependencies, this is how you send POST request purely in Python 3.
Sample output: