I'm trying to populate WP custom fields using the code below, my problem is I can't output the nested JSON field data.quote.USD. Can someone help find the right direction here? Thank you.
//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(';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
I'm trying to populate WP custom fields using the code below, my problem is I can't output the nested JSON field data.quote.USD. Can someone help find the right direction here? Thank you.
//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/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
Share
Improve this question
asked May 14, 2019 at 10:41
kriskrosskriskross
131 silver badge3 bronze badges
1 Answer
Reset to default 0I'm a bit confused by how the code relates, but here's how I would work with the data in the provided screenshot.
This is looping through the data array and accessing the quoted USD, if it exists.
I'm adding the prices to an array by the object's ID just to give you an example.
$prices = array();
foreach ( self::$remote_data['data'] as $data ) {
if ( ! isset( $data->quote ) || ! isset( $data->quote->USD ) ) {
continue;
}
$prices[ $data->id ] = $data->quote->USD->price;
}