PRO: oAuth Example

This pages shows how to get Access to JSON when a API uses the oAuth2 for authorization.

API-URL: http://api.json-content-importer.com/extra/api/bearer.php?a=w
This URL is expecting:

  • POST-Request
  • http-Header with “ACCESSTOKEN:token”

Why is that? bearer.php is build this way to demonstrate how this can be done with the JCI-Plugin.

Click on the above URL (this is GET, not POST) gives you a non-JSON response:

error: this API accepts POST-requests only!

This is not what we need to get access to the JSON-data.

Create a new JCI-Template with:

  • Name: oauth bearer request
  • URL: http://api.json-content-importer.com/extra/api/bearer.php?a=w
  • method: CURL-POST
  • httpstatuscodemustbe200=no tells the plugin to ignore http-errors (without only http-codes 200 are accepted) – if all is ok you can remove httpstatuscodemustbe200=no from the shortcode. Or leave it to catch errors.
[jsoncontentimporterpro nameoftemplate="oauth bearer request" httpstatuscodemustbe200=no]

twig-template:

{{_context | json_encode }}
<hr>Errormessage: {{_context.errormessage}} / time: {{_context.time}}

This gives us:

{"method":"POST","usedaccesstoken":"no token sent"} 
  • add to curloptions in the JCI-template:
    CURLOPT_HTTPHEADER=ACCESSTOKEN:whatever
  • API-response with this:
{"method":"POST","usedaccesstoken":"whatever","authorization":"failed"} 
[jsoncontentimporterpro nameoftemplate="gettoken"]

Received Token:

a029d0df84eb5549c641e04a9ef389e5

Now we use this Shortcode in the curloptions to replace “whatever” by the token: #BRO# and #BRC# stand for [ and ], which otherwise cause WordPress-Trouble…

CURLOPT_HTTPHEADER=ACCESSTOKEN:#BRO#jsoncontentimporterpro nameoftemplate=gettoken#BRC#

This gives us:

{"method":"POST","usedaccesstoken":"a029d0df84eb5549c641e04a9ef389e5","authorization":"ok","errormessage":"none","time":"Mon 08 2022, 12:42:00"}
Errormessage: none / time: Mon 08 2022, 12:42:00

PRO: Use Custom Post Fields for putting together a API-URL

A WordPress-Page can have some Custom Post Fields (CPF). By that one WP-page can be almost identical to another, the only differencewould be a CPF.
see also at https://doc.json-content-importer.com/json-content-importer/pro-use-custom-post-fields/


Example:

  1. The JSON-API http://api.json-content-importer.com/extra/json/meetup/1.json
    should be put together with CPF. So on one page 1.json is used, on another 2.json etc.. Triggered by CPF.
  2. Define “keyaa” and “keybb” as Customfields and set values “meetup” and “1.json” (e. g. ACF-Plugin)
  3. Set up a JCI-Template:
  • name: cpf-url
  • twig-template:
    wp_get_page_properties: get page ID
    wp_get_custom_field_value: use page ID and keyaa / keybb to get values of CPF
{% set pageprop = wp_get_page_properties(debug, pageid) %}	
{% set pageid = jcipageparam.post.ID %}
pageid: {{pageid}}<br>
{% set valkeyaa = wp_get_custom_field_value(pageid, 'keyaa') %}
CPF-keyaa: {{valkeyaa | json_encode }}<br>
{% set valkeybb = wp_get_custom_field_value(pageid, 'keybb') %}
CPF-keybb: {{valkeybb | json_encode }}<br>
{% set url = "http://api.json-content-importer.com/extra/json/"~valkeyaa.0~"/"~valkeybb.0~"" %}
url: {{url}}<br>
{% for item in _context %}
{{item.id}} {{item.name}} {{item.localized_name}}
{% endfor %}
  • URL: as 1st try use “dummyrequest”
    But the magic comes here – calc the URL:
{% set pageprop = wp_get_page_properties() %}
{% set pageid = pageprop.get_post.ID %}
{% set valkeyaa = wp_get_custom_field_value(pageid, 'keyaa') %}
{% set valkeybb = wp_get_custom_field_value(pageid, 'keybb') %}
{% set url = "http://api.json-content-importer.com/extra/json/"~valkeyaa.0~"/"~valkeybb.0~"" %}
{{url}}
  • Shortcode for that template:
[jsoncontentimporterpro nameoftemplate="cpf-url"]

This should show you

url: http://api.json-content-importer.com/extra/json/meetup/1.json

In Action:

pageid: 866
CPF-keyaa: ["meetup"]
CPF-keybb: ["1.json"]
url: http://api.json-content-importer.com/extra/json/meetup/1.json
Content from URL:
258386729 Love Breakfast
tcdgfqyzdbkb Drupal Coworking Day
258386398 Geeks - Februrary Edition
257739977 February 2019 Meetup
257183469 UG February Event
tpxglqyzdbsb Ignite Talks & Networking
258391683 public Event
fghgfhfghf D-Meetup






PRO: Pagination by twig – 1 feed, many pages

This page demonstrates how you can paginate data. All data comes in 1 feed (see here for data in several feeds)

Create a new JCI-Template for that:

{% set noonpage = 20 %}
{% set noofitems = ( days | length ) %}
{% if urlparam.no %}{% set selno = urlparam.no %}{% else %}{% set selno = 1 %}{% endif %}

number of data-items: {{ noofitems  }} at <a href="http://api.json-content-importer.com/extra/paginationdata.php" target="_blank">http://api.json-content-importer.com/extra/paginationdata.php</a><br>
{% set noofpages = (noofitems /noonpage) | round(0, 'ceil') %} show on {{noofpages}} pages:

{% set start = noonpage *(selno-1) %}
{% set end= start + noonpage %} 

{% for i in 1..noofpages %} 
{% if i==selno %} 
<b>this is page: </b> {{selno}} ({{start+1}} - {{end}})
{% else %} 
<a href=?no={{i}}>{{i}}</a>
{% endif %} 
{% endfor %}
<hr>
{% if noofitems ==0 %} No data-items found {% endif %} 
<ul>
{% for day in days | slice(start, noonpage) %} 
<li>
{% set currno = start + loop.index0+1 %} {{currno}}. {{day.FORENAMES}} {{day.SURNAME}}
</li>
{% endfor %} 
</ul>

Shortcode:

[jsoncontentimporterpro parser=twig nameoftemplate=pagination]

Example in action:

number of data-items: 165 at http://api.json-content-importer.com/extra/paginationdata.php
show on 9 pages: this is page: 1 (1 - 20) 2 3 4 5 6 7 8 9
  • 1. Johnson Williams
  • 2. Brown Jones
  • 3. Miller Davis
  • 4. Garcia Rodriguez
  • 5. Wilson Martinez
  • 6. Anderson Taylor
  • 7. Thomas Hernandez
  • 8. Moore Martin
  • 9. Jackson Thompson
  • 10. White Lopez
  • 11. Lee Gonzalez
  • 12. Harris Clark
  • 13. Lewis Robinson
  • 14. Walker Perez
  • 15. Hall Young
  • 16. Allen Sanchez
  • 17. Wright King
  • 18. Scott Green
  • 19. Baker Adams
  • 20. Nelson Hill