php - Api Json 数据不显示
问题描述
嗨,我在显示通过请求获取到外部 api rest 的 json 数据时遇到问题。我正在使用 wordpress 发出请求,并且返回的 json 文件没有错误,所以一切正常。当我尝试在 html 表中插入数据时,没有显示任何内容。下面我插入我正在使用的返回的 json 和 php 代码。
JSON
object(stdClass)#5501 (2) { ["success"]=> bool(true) ["data"]=> array(12) { [0]=> object(stdClass)#5562 (7) { ["sport_key"]=> string(26) "soccer_turkey_super_league" ["sport_nice"]=> string(19) "Turkey Super League" ["teams"]=> array(2) { [0]=> string(11) "Denizlispor" [1]=> string(16) "Yeni Malatyaspor" } ["commence_time"]=> int(1604831497) ["home_team"]=> string(16) "Yeni Malatyaspor" ["sites"]=> array(4) { [0]=> object(stdClass)#5641 (4) { ["site_key"]=> string(10) "paddypower" ["site_nice"]=> string(11) "Paddy Power" ["last_update"]=> int(1604835079) ["odds"]=> object(stdClass)#5643 (1) { ["h2h"]=> array(3) { [0]=> float(31) [1]=> float(1.06) [2]=> float(10) } } } [1]=> object(stdClass)#5647 (4) { ["site_key"]=> string(7) "betfair" ["site_nice"]=> string(7) "Betfair" ["last_update"]=> int(1604835077) ["odds"]=> object(stdClass)#5648 (2) { ["h2h"]=> array(3) { [0]=> float(26) [1]=> float(1.12) [2]=> float(10.5) } ["h2h_lay"]=> array(3) { [0]=> float(55) [1]=> float(1.13) [2]=> float(13) } } } ......``
PHP 脚本
add_shortcode( 'external_data', 'callback_function_name' );
function callback_function_name(){
$url = 'https://xxxxxxxxxx&apiKey=xxxxxxxxx';
$arguments = array(
'method' => 'GET',
);
$response = wp_remote_get($url, $arguments);
if ( is_wp_error($response)) {
$error_message = $response->get_error_message();
return "Something went wrong: $error_message";
}
$results = json_decode( wp_remote_retrieve_body( $response ));
//var_dump($results);
$html .='';
$html .= '<table>';
$html .='<tr>';
$html .='<td>Exemple<td>';
$html .='<td>Exemple<td>';
$html .='<td>Exemple<td>';
$html .='<td>Exemple<td>';
$html .='<td>Exemple<td>';
$html .='</tr>';
foreach( $results as $result ){
$html .='<tr>';
$html .='<td>' . $result->data . '<td>';
$html .='<td>' . $result->sport_key . '<td>';
$html .='<td>' . $result->sport_nice . '<td>';
$html .='<td>' . $result->teams . '<td>';
$html .='<td>' . $result->commence_time . '<td>';
$html .='</tr>';
}
$html .='</table>';
return $html;
}
?>
解决方案
您正在直接访问整个 JSON 响应,但您应该在循环data
的结果中访问。foreach
将您的foreach
循环更改为此,
foreach( $results['data'] as $result ){
$html .='<tr>';
$html .='<td>' . $result->sport_key . '<td>';
$html .='<td>' . $result->sport_nice . '<td>';
$html .='<td>' . $result->teams . '<td>';
$html .='<td>' . $result->commence_time . '<td>';
$html .='</tr>';
}
由于您需要的所有数据都在data
. 这是您的 JSON 的格式(就我从您的问题中得到的一样多。)
$result->teams
由于它是一个数组,因此您也无法直接打印。
在尝试解码之前,您应该使用JSON 美化器来检查响应并了解数据在 JSON 中的结构。
object(stdClass)#5501 (2) {
["success"]=> bool(true)
["data"]=> array(12) {
[0]=> object(stdClass)#5562 (7) {
["sport_key"]=> string(26) "soccer_turkey_super_league"
["sport_nice"]=> string(19) "Turkey Super League"
["teams"]=> array(2) {
[0]=> string(11) "Denizlispor"
[1]=> string(16) "Yeni Malatyaspor" }
["commence_time"]=> int(1604831497)
["home_team"]=> string(16) "Yeni Malatyaspor"
["sites"]=> array(4) {
[0]=> object(stdClass)#5641 (4) {
["site_key"]=> string(10) "paddypower"
["site_nice"]=> string(11) "Paddy Power"
["last_update"]=> int(1604835079)
["odds"]=> object(stdClass)#5643 (1) {
["h2h"]=> array(3) {
[0]=> float(31)
[1]=> float(1.06)
[2]=> float(10) }
}
}....
推荐阅读
- reactjs - 在没有日志的情况下测试 cypress 时登录挂起
- pandoc - Pandoc 转换为 PDF 不提供彩色超文本链接
- python - Python If 语句问题
- javascript - 我的 while 循环接受它不应该接受的答案。为什么?
- c# - 为什么我的页眉和页脚图像比例仅在我的机器上增加了 1000%?
- javascript - 将对象数组转换为树数据结构
- html - 如何在轮播Bootstrap中通过高度居中图像
- java - 如何存储来自 JTextField 的值并将其与 Thread 一起使用?
- python - Python3多处理终止vs杀死vs关闭
- r - 返回保存时如何清理函数闭包(环境)?