我一直在研究一种使用PHP显示来自API的信息的工具。到目前为止,我一直试图一次一个地回显各个信息领域,以便格式化它们。现在我很好奇我是否有可能以某种方式返回整个未解析的JSON返回,我的意思是它只是原始的JSON。我可以以某种方式将其称为PHP页面吗?
这是我所指的JSON的一个例子..
答案 0 :(得分:1)
如果您只想在网站上https://api.data.gov/sam/v4/registrations/...输出json
,可以使用:
header('Content-Type: application/json');
$url = "https:///someJsonUrl";
echo file_get_contents($url);
答案 1 :(得分:0)
您可以使用Client URL Library
。
以下是参考资料:
所以你的代码应该是这样的:
$ch = curl_init();
$searchURI = 'http://api.data.gov/sam/v4/registrations/7800853870000?return_values=full&api_key=CCHRrZMMc1s9I1dxtk4bwTPdoT4A1Xjck9w4Lii0';
curl_setopt($ch, CURLOPT_URL, $searchURI);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$jsonData=curl_exec($ch);
curl_close($ch);
// $jsonData is your row json data
header('Content-Type: application/json');
echo $jsonData;