从实时JSON填充自定义字段

时间:2019-05-14 作者:kriskross

JSON

我试图使用下面的代码填充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

1 个回复
SO网友:MikeNGarrett

我对代码的关系有点困惑,但下面是我如何处理所提供屏幕截图中的数据。

这是通过数据数组循环并访问报价美元(如果存在)。

我通过对象的ID将价格添加到数组中只是为了给您一个示例。

$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;
}

相关推荐

插件wp_end_json()中的AJAX发送html

我编写了一个插件,从JQuery调用PHP函数,PHP使用wp_send_json(). 这些函数都被成功调用,但json请求每次都会向jquery函数发送大量html。如何使php函数发送的json仅为特定消息?JQUERY:jQuery( document ).ready( function() { jQuery( \'body\' ).on( \'click\', \'.wpm_mail_link\', function( e ) {