PRO: oAuth2-Demo by using Shortcode in twig-code

This pages shows you how to get access to the data at
http://api.json-content-importer.com/extra/api/getdata.php?gd=acc

This API / URL expects:

  • Useragent: mozilla
  • HTTP-Header with “Authorization:Bearer TOKEN”
  • “TOKEN” has to be obtained from
    http://api.json-content-importer.com/extra/api/gettoken.php
    For that user / password has to be sent by CURL-POST.

This is called oAuth2, to avoid sending user / password via http, but a TOKEN the API can check if user / password is ok.

We need two JCI-Shortcodes for that.

JCI-Template 1: The Shortcode (orderofshortcodeeval=1 tells the plugin to first execute the twig code and then the Shortcode – 10 would not execute the Shortcode, then you can see the TOKEN…)

[jsoncontentimporterpro nameoftemplate="demo-oauth" orderofshortcodeeval=1]

is for the page which will show us the data.

Defined it like this:

  • Name: demo-oauth
  • URL: http://api.json-content-importer.com/extra/api/gettoken.php
  • method: CURL-POST
  • curloptions: CURLOPT_POSTFIELDS={“user”: “test”, “password”: “mypass”}
  • twig-template:
test<br>
[jsoncontentimporterpro nameoftemplate="demo-oauth2" curloptions="CURLOPT_USERAGENT=mozilla;CURLOPT_HTTPHEADER=Authorization:Bearer {{access_token}}"]

Ok, we need JCI-Template 2:

  • name: demo-oath2
  • URL: http://api.json-content-importer.com/extra/api/getdata.php?gd=acc
  • method: CURL-GET
  • curloptions: are sent from the other JCI-Template, where this Shortcode is used
  • twig-template:
{% if datasuccess %}
{% for b in boat %}
boat-id: {{b.id}}<br>
{% endfor %}
<hr>
{% for h in harbour %}
harbour-id: {{h.id}}<br>
{% endfor %}
{% endif %}

In Action:

test
boat-id: 3178749
boat-id: 2639985
boat-id: 3223573
boat-id: 1164130
boat-id: 6092316
boat-id: 6034499

harbour-id: 4594675
harbour-id: 1238000

PRO: Pagination by twig – several feeds, many pages

This page demonstrates how you can paginate data. The data comes in a several feeds (see here for data in one feeds)

Create a new JCI-Template for that:

  • Name of Template: paginate
  • URL:
{% set page = 0 %}{% if urlparam.no %}{% set page = urlparam.no-1 %}{% endif %}
http://api.json-content-importer.com/extra/api/pagination/?page={{page}}
  • urlparam4twig: no
  • twig template:
{% set noonpage = 10 %}
 {% set noofpages = (totalno/noonpage) | round(0, 'ceil') %}
no of found dataitems: {{totalno}}, show on {{noofpages}} pages, each with {{noonpage }} items
<br>
{% set page = 1 %}{% if urlparam.no %}{% set page = urlparam.no %}{% endif %}
{% for i in 1..noofpages %} 
{% if i==page %} 
//<b>this is page: </b> {{page }} //
{% else %} 
<a href=?no={{i}}>{{i}}</a>
{% endif %} 
{% endfor %}
{% set currentpage = 1 %}
{% if urlparam.no %}{% set currentpage = page %}{% endif %}
<br>{% set start = noonpage * (currentpage-1) +1 %}
{% set end = start + noonpage -1 %} 
{% if start < 1 %} {% set start = 1 %} {% set end = noonpage %} {% endif %} 
{% if end > totalno %} {% set end = totalno %} {% endif %} 
show {{start}} to {{end}}
<br>
{% for k,v in items %} 
{{k}}: {{v}}<br>
{% endfor %} 

Example in action:

no of found dataitems: 87, show on 9 pages, each with 10 items
1 2 3 4 5 6 7 //this is page: 8 // 9
show 71 to 80
71: e2c420d928d4bf8ce0ff2ec19b371514
72: 32bb90e8976aab5298d5da10fe66f21d
73: d2ddea18f00665ce8623e36bd4e3c7c5
74: ad61ab143223efbc24c7d2583be69251
75: d09bf41544a3365a46c9077ebb5e35c3
76: fbd7939d674997cdb4692d34de8633c4
77: 28dd2c7955ce926456240b2ff0100bde
78: 35f4a8d465e6e1edc05f3d8ab658c551
79: d1fe173d08e959397adf34b1d77e88d7
80: f033ab37c30201f73f142449d037028d

PRO: Send Form-Data to API



This is done in this way:
1. Create a 1st JCI-Template:
Name of Template: senddata2form
URL: dummyrequest (we don’t need a API when just displaying the Form)
urlparam4twig: inp
twig-template:

<form action=/send-form-data-to-api/>
<input type=text name=inp  value="{{urlparam.inp | e}}"><br>
<input type=submit value="Send Data">
</form>
{% if urlparam.inp %}
{% set sc = "[jsoncontentimporterpro nameoftemplate=senddata url=http://api.json-content-importer.com/extra/json/storeanswer.php?insert="~urlparam.inp~"]" %}
{% set scExe = (sc | doshortcode) %}
{{scExe}}
{% endif %}

Then use the following Shortcode on a page with URL /send-form-data-to-api/

[jsoncontentimporterpro nameoftemplate="senddata2form" orderofshortcodeeval=10]

2. In the above twig-template we call a JCI-Template named senddata. Let’S create this:
Name of Template: senddata
URL: empty (we send the URL in the Shortcode)
urlparam4twig: inp
twig-template:

hello senddata: {{nojsonvalue}}


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: 1 2 3 4 5 6 7 this is page: 8 (141 - 160) 9
  • 141. Delgado Valdez
  • 142. Pena Rios
  • 143. Douglas Sandoval
  • 144. Barrett Hopkins
  • 145. Keller Guerrero
  • 146. Stanley Bates
  • 147. Alvarado Beck
  • 148. Ortega Wade
  • 149. Estrada Contreras
  • 150. Barnett Caldwell
  • 151. Santiago Lambert
  • 152. Powers Chambers
  • 153. Nunez Craig
  • 154. Leonard Lowe
  • 155. Rhodes Byrd
  • 156. Gregory Shelton
  • 157. Frazier Becker
  • 158. Maldonado Fleming
  • 159. Vega Sutton
  • 160. Cohen Jennings