XF 2.1 JSON Decode and Insert

Cupara

Well-known member
I swear this worked in 2.0 but in 2.1 it doesn't seem to insert the data. I have tried to get a data dump so I could see the results but nothing appears since I'm running this through a cron. I did get it to work prior when I had errors and it was grabbing data fine. Anyway, here is the code, hopefully someone will see my mistake.

PHP:
public function insertRealms()
    {
        $options = \XF::options();
        $db = \XF::db();
        $app = \XF::app();

        try
        {
            $results = $app->http()->client()->get('https://us.api.blizzard.com/data/wow/realm/index?namespace=dynamic-us&locale=en_US&access_token=UShw2Mej5wyRMeaWgtP0qMKTbWyocEyIty');
        }
        catch(\GuzzleHttp\Exception\RequestException $e)
        {
            if (null !== $e->getResponse())
            {
                $error = 'Error' . $e->getResponse()->getStatusCode() . ' - ' . $e->getResponse()->getReasonPhrase();
            } else {
                $error = $e->getMessage();
            }
            
            return $error;
        }            

        $results = \GuzzleHttp\json_decode($results->getBody(), true);

        $realms = $results['realms'];
        
        foreach($realms AS $n)
        {
            $this->db()->insert('xwow_realms', [
                'realm_id'        => $n['id'],
                'realm_name'   => $n['name'],
                'realm_slug'   => $n['slug'],
                'realm_locale' => $options['xenwow_locale']
            ]);
        }
        
        return true;
    }

Thanks
 
Interesting, the Access Token changed. Great now I need to figure out the proper method otherwise this will be a flop.

EDIT: Figured it out. The reason it doesn't work is I need to run this code first.

PHP:
curl -u {client_id}:{client_secret} -d grant_type=client_credentials https://us.battle.net/oauth/token

Then use the generated access token to grab the data.
 
Last edited:
Top Bottom