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: 100095
boat-id: 239015
boat-id: 1055612
boat-id: 5381099
boat-id: 5874648
boat-id: 3113681

harbour-id: 6078547
harbour-id: 6324591

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
//this is page: 1 // 2 3 4 5 6 7 8 9
show 1 to 10
1: c4ca4238a0b923820dcc509a6f75849b
2: c81e728d9d4c2f636f067f89cc14862c
3: eccbc87e4b5ce2fe28308fd9f2a7baf3
4: a87ff679a2f3e71d9181a67b7542122c
5: e4da3b7fbbce2345d7772b0674a318d5
6: 1679091c5a880faf6fb5e6087eb1b2dc
7: 8f14e45fceea167a5a36dedd4bea2543
8: c9f0f895fb98ab9159f51fd0297e236d
9: 45c48cce2e2d7fbdea1afc51c7c6ad26
10: d3d9446802a44259755d38e6d163e820

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: 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