我遇到的问题似乎是在下面的执行函数中使用file_get_contents发送GET。对于我的生活,我无法弄清楚为什么。我使用POST测试了使用Postman的api服务器,它运行正常。
第一个函数用于传递我的请求以发送到服务器。第二个函数在我的form_handler.php文件中,它构建要通过execute()发送到服务器的数据。
api_handler.php
<?php
function execute($command, $context = NULL)
{
$url_scheme = "http://";
if (isset($_SERVER['HTTPS']))
{
$url_scheme = "https://";
}
$url_host = $_SERVER['HTTP_HOST'];
$url_api = "/SkedApi/";
$url = $url_scheme.$url_host.$url_api.$command;
echo $url . "<br/>";
var_dump($context);
var_dump(headers_list());
$response = file_get_contents($url, false, $context);
var_dump(headers_list());
echo $response;
if ($response)
{
$output = json_decode($response, true);
}
else
{
$output = "error";
}
return $output;
}
?>
users_handler.php
$test = array ('foo' => 'bar', 'bar' => 'baz');
$postdata = http_build_query($test);
echo $postdata;
$opts = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($postdata) . "\r\n",
'content' => $postdata
));
var_dump($opts);
$context = stream_context_create($opts);
print_r($context);
$result = execute("Users", $context);
if ($result)
{
echo "YES</br>";
}
从我的apache访问文件:
::1 - - [04/Jul/2017:16:02:00 -0400] "POST /SkedApi/Users HTTP/1.0" 301 239
::1 - - [04/Jul/2017:16:02:00 -0400] "GET /SkedApi/Users/ HTTP/1.0" 200 196
::1 - - [04/Jul/2017:16:02:00 -0400] "POST /SkedAvailability/users_handler.php HTTP/1.1" 200 2688
免责声明:抱歉所有的var_dumps;
答案 0 :(得分:0)
那么你的日志显示对POST请求的301响应,然后是对相同URL的200响应,并附加一个尾部斜杠。
如果更改了users_handler.php的这一行,会发生什么?
$result = execute("Users", $context);
对此?
$result = execute("Users/", $context);