PRO: Search multiple Websites – Call several APIs to collect results

Your Challenge:

The User
  • fills out one Searchform,
  • sends the request and
  • gets the searchresults from several websites.
The JCI-PRO plugin can do that, if the searched Servers have a JSON-Search-API. Usually WordPress-Website do have those. See how this is done with JCI-PRO:

Step 1: Searchresult Page

Place this Shortcode onto a ordinaray WordPress-Page:
[[jsoncontentimporterpro nameoftemplate=searchresults orderofshortcodeeval=1]]
  • Publish that page with e. g. URL “/search-jci-websites/”.
  • “orderofshortcodeeval” is available from Version 3.6.1 on. Before that: Set the Radiobutton at the JCI-PRO-Options in Tab “Shortcode-Settings” at “Usage of Shortcodes (JCI or other) in the JCI-twig-Template:” to “First option (default)…”

Step 2: Searchresult JCI-Template named “searchresults”

  • Name: searchresults
  • urlparam4twig : st#sno
  • URL of Template: dummyrequest (all API-requests are done with the following Template-Text by twig and JCI)
  • Template-Text:
    
    
    {% set sno = 10 %}{%if urlparam.sno %}{% set sno = urlparam.sno %}{% endif %}
    {%if sno >100 %}{% set sno = 100 %}{% endif %}
    {% set st = urlparam.st %}
    
    
    Show No of Hits (max. 100):
    {% macro displaySearchResult(resultArr) %} {% set hits = (resultArr | length) %} {% if hits > 1 %}
      {% for h in resultArr %} {% if h.title %}
    1. {{h.title}}
    2. {% endif %} {% endfor %}
    {% else %} nothing found {% endif %} {% endmacro %} {% macro doSearch(url, st, sno) %} {% set secwp = url~st~ "&per_page="~ sno %} {% set sc = '[[jsoncontentimporterpro nameoftemplate=searchsingle url='~secwp~']]' %} {% set scout=(sc |doshortcode ) %} {% set scoutJSON= scout| json_decode('TRUE') %} {{ _self.displaySearchResult(scoutJSON ) }} {% endmacro %} {% set sarray = { 'site1': { 'website': 'www.json-content-importer.com', 'url': 'https://www.json-content-importer.com', }, 'site2': { 'website': 'api.json-content-importer.com', 'url': 'https://api.json-content-importer.com', }, } %} {% for k,v in sarray %}

    {{v.website}}

    {{ _self.doSearch(v.url~"/wp-json/wp/v2/search?search=", st, sno ) }} {% endfor %}
    • Line 5: <form action="/search-jci-websites/"> Alter this depending on the URL you set for the Page in Step 1
    • Line 32ff: {% set sarray = { Alter this depending on the Websites you want to search
    • Line 46: {{ _self.doSearch(v.url~"/wp-json/wp/v2/search?search=", st, sno ) }} Alter this if the JSON-APIs works in another way…

Step 3: Receiving JCI-Template named “searchsingle”

Create a new JCI-Template:
  • Name: searchsingle
  • Template-Text: {{ _context | json_encode }} This gives us the pure JSON from the several Servers. If needed you can alter the received JSON here