duanji5116 2019-07-12 21:07
浏览 142

通过Python将数据发送到PHP

I'm having trouble while sending data to PHP script through python

if i try with browser then data is posting but if i try with python script it is not posting any data and no error is shown

i have tried using headers, get instead of post

PHP:

$q = @$_GET['q'];
if($q) {
   $sql = "INSERT INTO data VALUES ('', '".$q."')";
   if (mysqli_query($conn, $sql)) {
      echo "Posted";
   } else {
      echo "cannot post";
   }
}

PYTHON:

import requests
payload = {'q': 'some-data'}
r = requests.post('http://example.com/store.php', params=payload)
  • 写回答

1条回答 默认 最新

  • dopgl80062 2019-07-12 22:06
    关注

    Your Python requests method needs to match the one used by your PHP script to process the data.

    You are currently using POST method - requests.post() - in your Python script, while trying to retrieve the data sent in the query string on the PHP end with $_GET(). That won't work.

    You need to stay consistent...

    In your python script use a 'GET' request:

    r = requests.get('http://example.com/store.php', params=payload)
    

    You can then process the data with PHP using:

    $q = $_GET['q'];
    

    By the way, some free advice, do not use @ to suppress errors in PHP, it's bad practice.

    评论

报告相同问题?

悬赏问题

  • ¥15 lammps Gpu加速出错
  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)