首页 > 解决方案 > wp远程获取不显示响应卷曲

问题描述

我不是这方面的专家,但我正在努力学习。我在 youtube 上运行以下脚本,以展示游戏蜜蜂。

<?php
// If this file is access directly, abort!!!
defined( 'ABSPATH' ) or die( 'Unauthorized Access' );

// Action when user login into admin panel
add_shortcode( 'external_data', 'callback_function_name' );

function callback_function_name( $atts) {

    $defaults =[
         'title'=>'table title'
    ];

    $atts = shortcode_atts( 
        $defaults,
        $atts,
        'external_data'
    );

    $url = 'https://eu.wargaming.net/globalmap/game_api/clan/500057125/battles';
    
    $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);
    
    //qui abbiamo creato la tabella dei dati
    $html = '';
    $html .= '<h2>' .$atts ['title'] . '</h2>';
    $html .= '<table>';

    $html .= '<tr>';
    $html .= '<td>battle_time</td>';
    $html .= '<td>province_type</td>';
    $html .= '<td>arena_name</td>';
    $html .= '<td>province_name</td>';
    $html .= '</tr>';

    foreach ($results as $results) {    
       
       $html .= '<tr>';
       $html .= '<td>' . $results->battle_time. '</td>';
       $html .= '<td>' . $results->province_type.'</td>';
       $html .= '<td>' . $results->arena_name.'</td>';
       $html .= '<td>' . $results->province_name.'</td>';
       $html .= '</tr>';
    }

    $html .= '</table>';

     // questo mostra la tabella html
    return $html;

    
}

如果我点击链接,我会像这样卷曲:

  "clan": {
    "elo_rating_6": 1000,
    "elo_rating_10": 864,
    "name": "SQUADRONE CORAZZATO ITALIANO",
    "color": "#00a0e6",
    "elo_rating_8": 989,
    "tag": "SQCI",
    "appointed_battles_count": 1,
    "id": 500057125,
    "emblem_url": "https://eu.wargaming.net/clans/media/clans/emblems/cl_125/500057125/emblem_64x64_gm.png",
    "fine_level": 1
  },
  "planned_battles": [
    
  ],
  "battles": [
    {
      "battle_time": "2021-11-14 21:15:00.200000",
      "is_attacker": true,
      "province_revenue": 72,
      "province_id": "noupoort",
      "winner_id": null,
      "province_type": "landing",
      "battle_reward": 0,
      "attack_type": "TOURNAMENT",
      "arena_resp_number": 1,
      "province_owner_id": 500186929,
      "clan": {
        "division_id": 2741392,
        "arena_wins_percent": 40.0,
        "arena_battles_count": 140,
        "win_rating_delta": 8,
        "lose_rating_delta": -7
      },
      "enemy": {
        "division_id": 2741391,
        "elo_rating_6": 928,
        "elo_rating_10": 997,
        "name": "POLISH WHISKY DRINKERS",
        "arena_wins_percent": 50.0,
        "color": "#ff0303",
        "win_rating_delta": 7,
        "elo_rating_8": 1007,
        "arena_battles_count": 44,
        "tag": "_PWD_",
        "lose_rating_delta": -8,
        "id": 500070268,
        "emblem_url": "https://eu.wargaming.net/clans/media/clans/emblems/cl_268/500070268/emblem_64x64_gm.png",
        "fine_level": 1
      },
      "arena_name": "Redshire",
      "landing": true,
      "revenue_level": 0,
      "periphery": "EU2",
      "front_id": "season_17_eu_tier8m",
      "round_number": 1,
      "province_pillage_end_datetime": null,
      "province_name": "Noupoort"
    }
  ]

}

问题是我只看到创建的表。我究竟做错了什么?

标签: wordpress

解决方案


我几乎成功了,现在我遇到重复答案的问题......有什么建议吗?

$response = wp_remote_get( $url, $arguments );

if ( is_wp_error( $data ) ) {
    $error_message = $data->get_error_message();
    return "Something went wrong: $error_message";
} 
    
$data=json_decode( wp_remote_retrieve_body( $response) ,true) ;
//$results =json_decode(wp_remote_retrieve_body (str_replace('""','""',$response), true));
    
//qui abbiamo creato la tabella dei dati
$html = '';
$html .= '<h2>' .$atts ['title'] . '</h2>';
$html .= '<table>';

$html .= '<tr>';
$html .= '<td>battle_time</td>';
$html .= '<td>province_type</td>';
$html .= '<td>arena_name</td>';
$html .= '<td>province_name</td>';
$html .= '</tr>';


foreach ($data['battles'][0]as $battles) {  
   
   $html .= '<tr>';
   $html .= '<td> '. $data['battles'][0]['battle_time'] . '</td>';
   $html .= '<td>' . $data['battles'][0]['province_type'] .'</td>';
   $html .= '<td>' . $data['battles'][0]['province_id'].'</td>';
   $html .= '<td>' . $data ['battles'][0]['arena_name'].'</td>';
   $html .= '</tr>';
}

$html .= '</table>';

// questo mostra la tabella html
return $html;

}

battle_time province_type   arena_name  province_name
2021-11-16 21:15:00.000000  landing noupoort    Redshire
2021-11-16 21:15:00.000000  landing noupoort    Redshire
2021-11-16 21:15:00.000000  landing noupoort    Redshire
2021-11-16 21:15:00.000000  landing noupoort    Redshire
2021-11-16 21:15:00.000000  landing noupoort    Redshire
2021-11-16 21:15:00.000000  landing noupoort    Redshire
2021-11-16 21:15:00.000000  landing noupoort    Redshire

这是一个回应


推荐阅读