首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP访问JSON数据的本地API

问PHP访问JSON数据的本地API
EN

Stack Overflow用户
提问于 2020-03-19 11:06:14
回答 1查看 353关注 0票数 0

尝试从JSON数据中输出的本地API访问数据,但我一直得到:

file_get_contents(/var/www/html/api/api.php?action=stats&user=testplayer):未能打开

流:没有这样的文件或目录

代码:

代码语言:javascript
复制
<?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?

EN

回答 1

Stack Overflow用户

发布于 2020-03-19 11:21:45

如果读取文件内容,则无法处理php代码。

按以下方式更新$url:

代码语言:javascript
复制
$url = htmlspecialchars_decode($_SERVER["SERVER_NAME"] . "/api/api.php?action=stats&user=$user") ;

这将向apache服务器发出请求,而不是读取文件内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60755913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档