Access external JSON data and display in XF widget?

RobParker

Well-known member
I've got absolutely no idea where to start on this so if anyone can point me in the right direction that'd be great.

What I'd ultimately like to do is pull some of the data from http://www.football-data.org/index into widgets on XF.

I'm assuming I can do this in either JS or PHP but PHP would make most sense for XF.

Could someone please point me into the right direction? If there are examples already in XF that'd be great but I suspect there aren't.
 
I guess this can be quite generalised as well and even just a general JSON widget would be extremely useful.
 
We have some tools in XF which should make it easier. We have the use of Guzzle which is a full HTTP client implemented in PHP.

We use it a fair bit. One fairly simple example to look at is our various Captcha classes. Every single captcha we support has to either fetch some stuff from the captcha service to render it or submit some data back to it to verify. A good one to look at is XF\Captcha\TextCaptcha.

In essence, it is as simple as this:
PHP:
$client = \XF::app()->http()->client();
$response = $client->get("http://some.service.com/some/endoint.json")->json();
\XF::dump($response);
The first line gets the client object.

The second line performs a HTTP request to the given URL and then formats the response body as JSON. (It uses GET because of the get method, though if needed you could use the post method to do a POST request etc).

Which means when you get to the third line, you get the output of a standard PHP array representation of the JSON data. (So no need to convert it from the JSON string to an array).

What you will want to do is look at other areas (like the Captcha class) and look at other things, things like wrapping the request code in a try/catch to attempt to catch errors like the request failing. For something like that in your case you'd likely just want to return an error message in your widget or something similar. (We just silently log and skip the issue in the Captcha classes so it doesn't prevent registration).
 
Thanks!

They have a simple guzzle example for using their API

Code:
<?php
    require 'vendor/autoload.php';
    use GuzzleHttp\Client;  
    $client = new Client();

    $uri = 'http://api.football-data.org/v1/competitions/354/leagueTable';
    $header = array('headers' => array('X-Auth-Token' => 'YOUR_API_TOKEN'));
    $response = $client->get($uri, $header);          
    $json = $response->json();  
?>

I'm assuming that between that and your advice I should be able to get something working.

In general would it make sense to have a JSON widget type in XF? Or does you think anything like this is too specific for a standard widget?
 
The problem is that no two APIs are the same. You can see the differences between the two examples. The one I gave doesn't have any sort of authentication, whereas the example you gave needs to pass an API token in a specific header. That's not uncommon, but the method that is required differs greatly. Sometimes you might just pass the API key into the URL, sometimes it will be a header or sometimes it will be a header with a specific name. Sometimes you'll have to make an authorisation request with an API key, swap that for another key, and then make another request etc.

The permutations are far too vast to do anything so generic, but thankfully we can try to make it as easy as possible.

Their example looks good, but use the way I gave of calling the $client (\XF::app()->http()->client()).
 
Would you stick all of that into a html widget with php tags or should I use a php callback widget (and figure out what that means!)?
 
Would you stick all of that into a html widget with php tags or should I use a php callback widget (and figure out what that means!)?
A callback would probably be ideal. The name basically gives it away, you're calling back to a PHP class. In that class, you'd have a function which hits the API in question and grabs the data you're wanting to display. This data will then be sent back to the widget for you to use HTML/CSS and whatever else to format it and display it however you want.
 
Thanks

Is there any XF documentation or examples to guide me through it?

I'm assuming this is just about at my capability level if I can find a few examples :p
 
Back
Top Bottom