我遇到了一个非常奇怪的问题……在这个特定应用程序的远程web服务器上,当我尝试接收JSON输入(以任何方式)时,要么得到一个空的(“)返回。关于这个问题,奇怪的是它在我的本地机器上运行良好。当我说“工作正常”时,这意味着我可以通过Postman向本地机器的API端点发出请求,然后它就会执行它应该执行的操作。另一件奇怪的事情是,在这个web服务器上(在不同的文件中,但仍然是这个web服务器),我可以很好地接收JSON输入。
我也尝试了不同的方式将JSON数据发送到这个远程服务器。我对数据进行了原始卷曲,从Postman发送了数据,从Zapier的webhook s应用程序发送了数据,从其他应用程序的webhook功能发送了数据。所有这些不同的应用程序在尝试检索JSON数据时都给了我一个空返回。以下是我试图找回它的方法:
// index.php, the API endpoint processing file
<?php
declare(strict_types=1);
require_once '../../vendor/autoload.php';
require_once '../../model/model.php';
use ShinePHP\{Crud, CrudException, EasyHttp, EasyHttpException, HandleData, HandleDataException};
try {
$data = EasyHttp::turnJsonInputIntoArray();
} catch (EasyHttpException $ehe) {
echo json_encode(['status' => 'failure', 'message' => $ehe->getMessage()]);
exit;
}
这是
turnJsonInputIntoArray()
EasyHttp类中的方法:
public static function turnJsonInputIntoArray(string $urlToRetrieveFrom = 'php://input') : array {
// Check if JSON is null, if it is, throw EasyHttpException, if not, return the decoded assoc array.
if (json_decode(file_get_contents($urlToRetrieveFrom), true) === null) {
throw new EasyHttpException('No data retrieved from url: '.$urlToRetrieveFrom);
} else {
return json_decode(file_get_contents($urlToRetrieveFrom), true);
}
}
我甩了瓦鲁
$_POST
(它返回了一个空数组),
$HTTP_RAW_POST_DATA
(返回空值),并且
file_get_contents('php://input')
(返回空字符串)。NGIX错误日志中除了存在的自定义异常之外,没有任何其他内容存在,因为PHP://Posits中没有任何内容。什么都没用,我真的不知道如何接收我的JSON数据。
如何将JSON数据接收到运行在远程服务器上的PHP脚本中?