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: 2948694
boat-id: 5664051
boat-id: 5192414
boat-id: 3954207
boat-id: 2548959
boat-id: 919346

harbour-id: 1222691
harbour-id: 5400520

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 //this is page: 3 // 4 5 6 7 8 9
show 21 to 30
21: 3c59dc048e8850243be8079a5c74d079
22: b6d767d2f8ed5d21a44b0e5886680cb9
23: 37693cfc748049e45d87b8c7d8b9aacd
24: 1ff1de774005f8da13f42943881c655f
25: 8e296a067a37563370ded05f5a3bf3ec
26: 4e732ced3463d06de0ca9a15b6153677
27: 02e74f10e0327ad868d138f2b4fdd6f0
28: 33e75ff09dd601bbe69f351039152189
29: 6ea9ab1baa0efb9e19094440c317e21b
30: 34173cb38f07f89ddbebc2ac9128303f

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 this is page: 3 (41 - 60) 4 5 6 7 8 9
  • 41. Cruz Hughes
  • 42. Price Myers
  • 43. Long Foster
  • 44. Sanders Ross
  • 45. Morales Powell
  • 46. Sullivan Russell
  • 47. Ortiz Jenkins
  • 48. Gutierrez Perry
  • 49. Butler Barnes
  • 50. Fisher Henderson
  • 51. Coleman Simmons
  • 52. Patterson Jordan
  • 53. Reynolds Hamilton
  • 54. Graham Kim
  • 55. Gonzales Alexander
  • 56. Ramos Wallace
  • 57. Griffin West
  • 58. Cole Hayes
  • 59. Chavez Gibson
  • 60. Bryant Ellis