尝试从JSON数据中输出的本地API访问数据,但我一直得到:
file_get_contents(/var/www/html/api/api.php?action=stats&user=testplayer):未能打开
流:没有这样的文件或目录
代码:
<?php
error_reporting(-1);
ini_set("display_errors", 1);
include 'functions.php';
if(isset($_GET['action']))
{
header('Content-Type: application/json');
try
{
switch($_GET['action'])
{
case 'stats':
if(!isset($_GET['user']))
throw new Exception('Insufficient information : Username');
$user = $_GET['user'];
$url = htmlspecialchars_decode("/var/www/html/api/api.php?action=stats&user=$user");
$response = file_get_contents("$url");
$data = json_decode($response);
echo $data;
break;
}
}
catch(Exception $e)
{
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(array('result' => 'error', 'cause' => $e->getMessage()));
}
}
?>难道不可能通过吗?和&s通过file_get_contents?
发布于 2020-03-19 11:21:45
如果读取文件内容,则无法处理php代码。
按以下方式更新$url:
$url = htmlspecialchars_decode($_SERVER["SERVER_NAME"] . "/api/api.php?action=stats&user=$user") ;这将向apache服务器发出请求,而不是读取文件内容。
https://stackoverflow.com/questions/60755913
复制相似问题