读取简单的 API 返回空白屏幕?

出于某种原因,我无法弄清楚如何在任何地方在线解决这个问题?我有这个简单的免费查克诺里斯笑话 api,我只是想输出它的 json,但它不起作用?我只是得到一个空白屏幕?


在控制台中,网络选项卡显示 200 ok 并且标头有效?我究竟做错了什么?


我的代码:


<?php



  header('Content-Type: application/json');

  header('X-RapidAPI-Host: matchilling-chuck-norris-jokes-v1.p.rapidapi.com');

  header('X-RapidAPI-Key: 341b5c1156msh6827bf7184ef4ddp1c8d09jsnbc52db5d01be');




  $str = file_get_contents('https://matchilling-chuck-norris-jokes-v1.p.rapidapi.com/jokes/random');


// decode JSON

$json = json_decode($str, true);


// get the data


print_r($json);


浮云间
浏览 93回答 1
1回答

慕无忌1623718

您很可能想发送这些标头...header在本地打印标题给你,你在哪里执行它。有了file_get_contents它会是这样的:<?php$opts = [&nbsp; &nbsp; "http" => [&nbsp; &nbsp; &nbsp; &nbsp; "method" => "GET",&nbsp; &nbsp; &nbsp; &nbsp; "header" => "Content-Type: application/json\r\n" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "X-RapidAPI-Host: matchilling-chuck-norris-jokes-v1.p.rapidapi.com\r\n" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "X-RapidAPI-Key: 341b5c1156msh6827bf7184ef4ddp1c8d09jsnbc52db5d01be\r\n"&nbsp; &nbsp; ]];$context = stream_context_create($opts);$str = file_get_contents('https://matchilling-chuck-norris-jokes-v1.p.rapidapi.com/jokes/random', false, $context);// decode JSON$json = json_decode($str, true);// get the dataprint_r($json);我还建议切换到curl扩展。
打开App,查看更多内容
随时随地看视频慕课网APP