我尝试了两个找到here的解决方案,但似乎没有形成我
我的请求是这样构建的:
<?php
class RequestObject {
public $p1 = 1;
public $p2 = 2;
}
$requestObject = new RequestObject();
$requestString = http_build_query($requestObject);
// ^^ returns 'p1=1&p2=2'
// $requestUrl = "http://address/api?fn=li_con"; // old with failure response
$requestUrl = "http://address/api/?fn=li_con";
$requestParameters = array(
"http" => array(
"method" => "POST",
"header" => "Content-type: application/x-www-form-urlencoded",
"content" => $requestString
)
);
$streamContext = stream_context_create($requestParameters);
$result = file_get_contents($requestUrl, false, $streamContext);
echo "$result";
我的api做了一个简单的post和get数组响应为json string:
<?php
switch(filter_input(INPUT_GET, 'fn')) {
case 'li_con' :
echo json_encode(array($_POST, $_GET));
break;
default:
echo "action not found";
}
所有我回来的都是这个字符串:
[[],{"fn":"li_con"}]
那么,有什么不对?为什么我没有在我的api中获得post参数?
答案 0 :(得分:1)
一个小错误:在;
行添加echo "action not found"
。它在测试时给了我错误。
除此之外,它在我的本地测试中运行良好。我使用PHP开发服务器,这是我的结果(我删除了div
标签):
[{"p1":"1","p2":"2"},{"fn":"li_con"}]
我使用的是带有PHP 7.0.10的Arch Linux。也许您的环境有问题?请提及PHP版本,Web服务器及其配置,以及如何测试客户端脚本。