我试图通过URL将python文件中的JSON发送到PHP文件。正确调用并运行PHP文件。但它没有捕获我尝试发送的msg数据。但它显示为NULL。
myPython.py
import requests, json
url = 'http:/mywebsite.com/api/v1/myPhpFile.php?msg='
payload = {'firstname':'Olivia', 'city':'London', 'country':'UK'}
r = requests.post(url, data=payload)
也尝试过:
r = requests.post(url, data=json.dumps(payload))
myPHP.php
<?php
...
class AddUser extends Abstractor {
public $log;
public function __construct() {
$this -> log = new MyLogPHP('../includes/debug.log.txt');
}
public function processJob($workerMsg) {
$this -> log -> info($workerMsg, 'W/ADD-USER');
}
}
$addUser = new AddUser();
$response = $addUser -> processJob($_REQUEST['msg']);
在日志中,我得到NULL值。
我甚至尝试通过删除&#39;?msg =&#39;
来修改网址myPython.py
import requests, json
url = 'http:/mywebsite.com/api/v1/myPhpFile.php'
payload = {'msg':{'firstname':'Olivia', 'city':'London', 'country':'UK'}}
r = requests.post(url, data=payload)
或
r = requests.post(url, data=json.dumps(payload))
我在这里做错了什么?
答案 0 :(得分:0)
@Alex是对的。
您正在发送正文中的数据,您应该使用PHP请求正文。 下面的代码将帮助您入门。
<强> myPython.py 强>
import requests, json
url = 'http:/mywebsite.com/api/v1/myPhpFile.php'
payload = {'firstname':'Olivia', 'city':'London', 'country':'UK'}
r = requests.post(url, data=json.dumps(payload))
<强> myPHP.php 强>
<?php
...
class AddUser extends Abstractor {
public $log;
public function __construct() {
$this -> log = new MyLogPHP('../includes/debug.log.txt');
}
public function processJob($workerMsg) {
$this -> log -> info($workerMsg, 'W/ADD-USER');
}
}
$addUser = new AddUser();
$entityBody = file_get_contents('php://input');
$response = $addUser -> processJob($entityBody);