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: 3342703
boat-id: 5656884
boat-id: 7525721
boat-id: 6045673
boat-id: 5259350
boat-id: 3241446

harbour-id: 419040
harbour-id: 5576412

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 //this is page: 6 // 7 8 9
show 51 to 60
51: 2838023a778dfaecdc212708f721b788
52: 9a1158154dfa42caddbd0694a4e9bdc8
53: d82c8d1619ad8176d665453cfb2e55f0
54: a684eceee76fc522773286a895bc8436
55: b53b3a3d6ab90ce0268229151c9bde11
56: 9f61408e3afb633e50cdf1b20de6f466
57: 72b32a1f754ba1c09b3695e0cb6cde7f
58: 66f041e16a60928b05a7e228a89c3799
59: 093f65e080a295f8076b1c5722a46aa2
60: 072b030ba126b2f4b2374f342be9ed44

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 this is page: 6 (101 - 120) 7 8 9
  • 101. Hudson Duncan
  • 102. Armstrong Berry
  • 103. Andrews Johnston
  • 104. Ray Lane
  • 105. Riley Carpenter
  • 106. Perkins Aguilar
  • 107. Silva Richards
  • 108. Willis Matthews
  • 109. Chapman Lawrence
  • 110. Garza Vargas
  • 111. Watkins Wheeler
  • 112. Larson Carlson
  • 113. Harper George
  • 114. Greene Burke
  • 115. Guzman Morrison
  • 116. Munoz Jacobs
  • 117. Obrien Lawson
  • 118. Franklin Lynch
  • 119. Bishop Carr
  • 120. Salazar Austin