首页 > 解决方案 > 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;


}


?>

标签: phpjsonapiparsingwordpress-rest-api

解决方案


您正在直接访问整个 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) } 
               } 
             }....

推荐阅读