0

I tried requests module but I've error in my php outputs: The error is: Notice: Undefined index: firstname in D:\xampp\htdocs\myfile\receive.php on line 7 NULL

code in python:
'''
payload = {'firstname':'Jack'}
url = "http://localhost/myfile/receive.php"
resp = requests.post(url, data=payload)
print(resp.status_code)
text =resp.text
print(text)
'''
status_code is ok(200)!

php code:
$data = $_POST["firstname"];
var_dump($data);

It seems okay but it's not :)

  • Try `var_dump($_POST);` to see what you get in php script. – Lessmore Sep 22 '21 at 02:45
  • please use isset() when handling POST / GET / COOKIE / SESSION variables – Ken Lee Sep 22 '21 at 02:53
  • Not sure what the python code will send your payload as - is it sending it as JSON, perhaps? Then you would have to receive it differently in PHP - https://stackoverflow.com/questions/18866571/receive-json-post-with-php – CBroe Sep 22 '21 at 08:05
  • P.s., please go read https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks – CBroe Sep 22 '21 at 08:05
  • @Lessmore the output of python is:
    'array(1) { ["firstname"]=> string(4) "jack" } '
    but my browser shows me: ' array(0) { }'
    – joker_007 Sep 23 '21 at 03:48
  • @KenLee I tried this 'if(isset($_POST['firstname'])){ var_dump($_POST['firstname']); echo 'P'; }else{ echo 'There is nothing to show'; }'
    the output in python was:' string(4) "jack" ' but in browser : There is nothing to show!
    – joker_007 Sep 23 '21 at 03:54

1 Answers1

0

In your php code, you have to use $_REQUEST["firstname"];

$data = $_REQUEST["firstname"]

instead of using $_POST

vimuth
  • 5,064
  • 33
  • 79
  • 116
Marco
  • 1