我试图使用下面的代码填充WP自定义字段,我的问题是我无法输出嵌套的JSON字段数据。引用美元。有人能帮我找到正确的方向吗?非常感谢。
//some code here
private function sanitize_remote_data($data_as_array)
{
foreach ($data_as_array as $inx => $data) {
$data_as_array [$inx] = sanitize_text_field($data);
}
return $data_as_array;
}
private function parse_data($dirty_data_as_array)
{
$clean_data = $this->sanitize_remote_data($dirty_data_as_array);
$this->raw = $clean_data;
$this->CoinName = $this->raw [\'name\'];
$this->CoinName = $this->raw [\'name\'];
$this->Symbol = $this->raw [\'symbol\'];
$this->Id = $this->raw [\'id\'];
$this->Circ_Supply = $this->raw [\'circulating_supply\'];
//$this->Price = $this->raw [\'price\'];
}
} // RemoteCoin
endif; // RemoteCoin
if (!class_exists(\'RemoteCoins\')) :
class RemoteCoins
{
public static $remote_data;
public static $coins;
private static function GetRemoteCoins($url)
{
$response = wp_remote_get($url);
if (is_wp_error($response) || !isset($response[\'body\'])) return;
$body = wp_remote_retrieve_body($response);
if (is_wp_error($body)) return; // bad body
$data = json_decode($body, true);
if (!$data || empty($data)) return; // bad data
if (isset($data[\'page\']) && isset($data[\'no_of_pages\'])) {
$page = $data[\'page\'];
$pages = $data[\'no_of_pages\'];
$next_page_link = $data[\'next_page_link\'];
if ($page !== $pages) {
}
}
return $data;
}
public static function LoadRemoteCoins($to_create_posts)
{
self::$remote_data = self::GetRemoteCoins(\'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?limit=5&sort=market_cap&CMC_PRO_API_KEY=***\');
if (!isset(self::$remote_data [\'data\'])) return; // no rows --- booo!
$works = array();
foreach (self::$remote_data [\'data\'] as $inx => $remote_item_data) {
$coin = new RemoteCoin($remote_item_data);
$works[] = $coin;
}
self::$coins = $works;
if ($to_create_posts) {
$count = 0;
foreach (self::$coins as $inx => $coin) {
$count++; // for demo ---- let\'s not go overboard here
$post_id = self::CreateLocalCoinPost($coin);
if ($count >= 5) return self::$remote_data;
}
}
return self::$remote_data;
}
//some code here