Task: Display all players from the Money Ranking (using api.snooker.org/?rt=[RankingType]&s=[Season]) and retrieve their names from the feed at http://api.snooker.org/?p=PLAYERID.
Solution: Get the list, where only IDs of the players are listed. The loop the playerlist and get the player-details via player-ID.
See how this works:
- Step 1: Get a Token (“TOKEN_YOU_GOT_FROM_SNOOKER.ORG”) from https://api.snooker.org/
This token has to be included in every API request, sent in the HTTP headerX-Requested-By:TOKEN_YOU_GOT_FROM_SNOOKER.ORG - Step 2: Create a JCI-Template for Player-Details
URL: https://api.snooker.org/?rt=MoneyRankings&s=2015
Curloptions: CURLOPT_HTTPHEADER=X-Requested-By:TOKEN_YOU_GOT_FROM_SNOOKER.ORG
Method of API-request: CURL-GET
Cachetime: Plugin caches this request to reduce API-requests. Cachetime in seconds, e. g. 86400 for 1 day
Name of Template: playerlist
twig-template:
{% for i in _context | slice(0,5) %}
{% if i.PlayerID > 0 %}
{% set sc = '[jsoncontentimporterpro nameoftemplate="playerdetails" url="https://api.snooker.org/?p='~i.PlayerID~'"]' %}
{% set scexec = (sc | doshortcode ) %}
{% set playerdetails = (scexec | json_decode(TRUE) ) %}
<img src="{{playerdetails.0.Photo}}" width="200"><br clear="all">{{i.Position}}. <a href="https://api.snooker.org/?p={{i.PlayerID}}" target="_blank">{{playerdetails.0.FirstName}} {{playerdetails.0.MiddleName}} {{playerdetails.0.LastName}}, Birthdate: {{playerdetails.0.Born |date("d.m.Y") }}, (PlayerID: {{i.PlayerID}})</a>
<br clear="all">
{% endif %}
{% endfor %}
Use this Shortcode (orderofshortcodeeval=10 tells the plugin not to execute the Shotcode in the twig-code):
[jsoncontentimporterpro nameoftemplate="playerlist" orderofshortcodeeval=10]
This provides a list of the first five players (using the slice filter) along with their PlayerID and a link to the JSON file containing more details. Next, we need to retrieve this JSON player detail data and add it to the ranking. To do this, we’ll query the API again using another JCI shortcode.
- Step 3: Add another JCI-Template:
URL: leave blank
Curloptions: CURLOPT_HTTPHEADER=X-Requested-By:TOKEN_YOU_GOT_FROM_SNOOKER.ORG
Method of API-request: CURL-GET
Cachetime: Plugin caches this request to reduce API-requests. Cachetime in seconds, e. g. 86400 for 1 day
Name: playerdetails
twig-template:
{{ _context | json_encode }}
- In Action: Example output (images not rendered in this view).
1. Mark Selby, Birthdate: 19.06.1983, (PlayerID: 17)
2. Stuart Bingham, Birthdate: 21.05.1976, (PlayerID: 30)
3. Judd Trump, Birthdate: 20.08.1989, (PlayerID: 12)
4. Shaun Murphy, Birthdate: 10.08.1982, (PlayerID: 97)
5. Neil Robertson, Birthdate: 11.02.1982, (PlayerID: 154)