WPRM Example 4: Mashape APIs – Market Data API

You can access the Mashape APIs website from here. Today, we will implement MARKET DATA API.

  1. Establishing Connection:
    To make a request to the service server you have to use a URL which have 2 parts: base url and parameters. The first part is static and the second is dynamic in most cases which means that you can change the results if you changed the parameters’ values.
    To establish a connection you need to know 3 thing about your API:

    • Its base url (your custom while 1 and 2 can be replaced with the paraameters): https://stockviz.p.mashape.com/marketdata/{1}
    • the results (response) format [Json, XML, etc ..]: JSON in this example
    • The parameters Keys: the name of the parameters like data (or you can all it data_type).
    • Check the checkbox to confirm that your url is dynamic with numbers like {1}.
    • In Authorization and header settings section, You need to add X-Mashape-Key as token key. Get one using the API’s documentation.

    Once you now that, go to Restful Manager > Connection, add a new connection, give it a relevant name(you will need it soon)  and add the data as in the following screenshot then click Save.
    mashconn

  2. Create request and Connection Settings:
    Next, we need to assign those parameters to suitable data so that the web service can query it and return the required data response.
    Now, go to Restful Manager > Requests, add a new request. and give it a name, then select the connection name that you have just created. When the parameters show up, populate the parameter values as you see in the following screenshot. Both are fixed values(however you can make it so it retrieve from database).
    As you can see, there are a lot of options in Parameter values setting, most of them are to set the dynamic data retrieval which is one of the features that makes this plugin powerful(we will discuss it more in other tutorial).
    Once you populated those fields, click save.
    mashreq
  3. Request’s Display Settings:
    After you save your values, you will find new sections are generated in the request page. One of them is ‘Display Settings’ which responsible for what you want to do with the response results:

    • Display Vs. Save then Display: WPRM give a chance to save selected items from the results in database or just display it in runtime. Most of times we will just choose ‘Display Only’.
    •  Where to display results: decide where to show the results template by selecting post type and then before or after content or you can just use a shortcode to add it wherever you like. Choose ‘shortcode’ and save the code that shows up.
    • Showing repeatable results: select where or not to loop through results. This used only when you have a group of results that share the same group of parameter keys which is the case here.
    • Start Checking dummy data: add all values in this section with full fixed(no dynamic work is here) dummy(but correct) data then click ‘check request’ once you see results check only the data that you want it to show up in your app or widget.
      mashreqdis
  4. Templating Results:
    Now every thing is ready except how the results will look like!! so here comes the last section, ‘Template Settings’. You will find in it 2 parts:

    • Header or non-repeatable Template: this is the template that show results one time. you can add text inline style and html to template you results. The links below the editor is a shorthand to help you copy and paste to the editor:
      • the results keys
      • Functional helpers like: if/ifExists/section
    • Repeatable template: if you have a repeatable data you will setup this template exactly as the above to be in for each repeated item at a time.mashreqtemp
      Save the changes once you finish.
      mashtest

To see the output, we’ll copy and paste the shortcode from the display section in any text widget in your theme. It’s very simple but you can style it as you like.

WPRM Example 3: Railways API

You can access the API website from here.

  1. Establishing Connection:
    To make a request to the service server you have to use a URL which have 2 parts: base url and parameters. The first part is static and the second is dynamic in most cases which means that you can change the results if you changed the parameters’ values.
    To establish a connection you need to know 3 thing about your API:

    • Its base url (your custom while 1 and 2 can be replaced with the paraameters): http://api.railwayapi.com/name_to_code/station/{1}/apikey/{2}/
    • the results (response) format [Json, XML, etc ..]: JSON in this example
    • The parameters Keys: the name of the parameters like station_name & api_key.
    • Check the checkbox to confirm that your url is dynamic with numbers like {1}.

    Once you now that, go to Restful Manager > Connection, add a new connection, give it a relevant name(you will need it soon)  and add the data as in the following screenshot then click Save.
    railconn

  2. Create request and Connection Settings:
    Next, we need to assign those parameters to suitable data so that the web service can query it and return the required data response.
    Now, go to Restful Manager > Requests, add a new request. and give it a name, then select the connection name that you have just created. When the parameters show up, populate the parameter values as you see in the following screenshot. Both are fixed values(however you can make it so it retrieve from database).
    As you can see, there are a lot of options in Parameter values setting, most of them are to set the dynamic data retrieval which is one of the features that makes this plugin powerful(we will discuss it more in other tutorial).
    Once you populated those fields, click save.
    railreq
  3. Request’s Display Settings:
    After you save your values, you will find new sections are generated in the request page. One of them is ‘Display Settings’ which responsible for what you want to do with the response results:

    • Display Vs. Save then Display: WPRM give a chance to save selected items from the results in database or just display it in runtime. Most of times we will just choose ‘Display Only’.
    •  Where to display results: decide where to show the results template by selecting post type and then before or after content or you can just use a shortcode to add it wherever you like. Choose ‘shortcode’ and save the code that shows up.
    • Showing repeatable results: select where or not to loop through results. This used only when you have a group of results that share the same group of parameter keys which is the case here. so select this option.
    • Repeatable Results Root: this is the root to loop through results(will com back to it later).
    • Start Checking dummy data: add all values in this section with full fixed(no dynamic work is here) dummy(but correct) data then click ‘check request’ once you see results check only the data that you want it to show up in your app or widget(it’s enough to check the data from one repeatable and the system will get them all). Also, for the ‘Repeatable Results Root’ that we left it earlier, find the parent of the Repeatables and add it to this field as you see in the screenshot both highlighted with yellow.
      railreqdis
  4. Templating Results:
    Now every thing is ready except how the results will look like!! so here comes the last section, ‘Template Settings’. You will find in it 2 parts:

    • Header or non-repeatable Template: this is the template that show results one time. you can add text inline style and html to template you results.
    • Repeatable template: if you have a repeatable data you will setup this template exactly as the above to be in for each repeated item at a time. The links below the editor is a shorthand to help you copy and paste to the editor:
      • the results keys
      • Functional helpers like: if/ifExists/section

      railtemp
      Save the changes once you finish.

railtestTo see the output, we’ll copy and paste the shortcode from the display section in any text widget in your theme. It’s very simple but you can style it as you like.

WPRM Example: Klout SEO API

In this example, we will validate a phone/mobile number using Klout API. Please, make sure that:

  • WP Restful Manager Plugin installed and active.
  • You read our quick launch example(Basic steps will not explained in this example).
  • Know what this API does.

In this API, we will go through 8 steps:

  1. Sign up in the API and get the API key.
  2. Establish a connection to get Klout Network Id.
  3. Create data request and Populate parameters values.
  4. Set data storing options.
  5. Establish a new connection for koultData.
  6. Create request and populate parameters values.
  7. Set display options.
  8. Customize the display template.
  1. Sign up in the API and get the API key:
    Go to documentation, sign up and get you API key.koult
  2. Establish a connection to get Klout Network Id:
      • Connection base url: http://api.klout.com/v2/identity.json/twitter
      • the results (response) format: json
      • The parameters Keys: key/ screenName

    These will be added as in the screenshot then save it:
    conk

  3. Create request and Populate parameters values:
    • key: it will be fixed value. then copy and paste the API key in the value source.
    • screenName: it could be a fixed value if you want to add fixed tweeter id. In this example, i assume that you store your users’ tweeter id as users meta so we will retrieve them dynamically.

    Save after you finish.
    reqk

  4. Set data storing options:
    • Display/store: We will only save the targeted output in the database output.
    •  Where to display results: If you use a fixed twitter id you can choose ‘save only’. But since it will be dynamic so we will use shortcode here.
    • Showing repeatable results: don’t check this option we don’t have repeatable data.
    • (save everything before this step)Start Checking dummy data: add all values in this section with full fixed(no dynamic work is here) dummy(but correct) data then click ‘check request’ once you see results check only the data that you want save which in this case will be the id. A dropdwon menu show up choose where to save. In this case will be user meta, select it then memorize the meta key that will show beside it.reqkd. once When you’re finished, click save.
  5. Establish a new connection for koultData.
    • Connection base url: http://api.klout.com/v2/user.json/{1}/{2} (this is called custom url. To provide flexibility, we made it easy to add parameters in the base url itself. The screenshot below will clear it out).
    • the results (response) format: json
    • The parameters Keys: networkid/type/key(the custom parameters’ keys can be given random names – names are only references here – but the key value parameters have to be given the same name as in the documentation).
    • activate the custom URLs checkbox.

    These will be added as in the screenshot then save it:
    conk2

  6. Create data request and populate parameters values:
    • networkid: choose ‘user meta’ and select the user meta key that you have just saved it in step #4.
    • type: if you had a look at the docs. You will find several types of data you can retrieve like Score, Influence, Topics, etc .. here we will choose score.
    • key: it will be fixed value. then copy and paste the API key in the value source(same as above).
      reqk2
  7. Set data storing options in the new request:
    • Display/store: here we will display data so select ‘Display Only’.
    •  Where to display results: we will use shortcode here.
    • Showing repeatable results: don’t check this option we don’t have repeatable data.
    • (save everything before this step)Start Checking dummy data: add all values in this section with full fixed(no dynamic work is here) dummy(but correct) data then click ‘check request’. You can get the ‘networkid’ from the previous request just by copy and paste it. Check whatever you want to display.

    reqkd2

  8. Create data request and populate parameters values:
    Now every thing is ready except how the results will look like!! so here comes the last section, ‘Template Settings’.

    • In Header or non-repeatable Template: this is the template that we’ll use. you can add text inline style and html to template you results. The links below the editor is a shorthand to help you copy and paste to the editor:
      • the results keys: score/bucket.
      • Functional helpers like: if/ifExists/sections

      In the screenshot below, we create a table then add the score of the twitter account at first row then the level of the score on the second row. We get the level by checking the bucket if it between 30-39, it will show Bad, and if it between 50-59, it shows Moderate, and so on. You can add as many [if] conditions as you like. reqktemp

    • Save the changes. After that get both requests shortcodes [rm req=xx] and add them in whatever page or post you like. Make sure you add them in the same order we created them so we can store the network id first. The screenshot below shows how it would look like. It’s so simple but it could be effective in real app or website.
      sampsk

WPRM Example: Phone number validation API

In this example, we will validate a phone/mobile number using numverify API. Please, make sure that:

  1. Establishing Connection:
    If you checked the API’s documentation, you will find that to make a request to the service server you have to use a URL which have 2 parts: base url (http://apilayer.net/api/validate) and parameters (access_key=YOUR_ACCESS_KEY&number=14158586273). The first part is static and the second is dynamic in most cases which means that you can change the results if you changed the parameters’ values.
    To establish a connection you need to know 3 thing about your API:

    • Its base url.
    • the results (response) format [Json, XML, etc ..].
    • The parameters Keys: the name of the parameters like access_key & currencies.

    Once you now that, go to Restful Manager > Connection, add a new connection, give it a relevant name(you will need it soon)  and add the data as in the following screenshot then click Save.
    Note: You can get access key to the API by signing up for a free plan.
    conn2

  2. Create request and Connection Settings:
    Next, we need to assign those parameters to suitable data so that the web service can query it and return the required data response. In our example, we’ll 2 parameter values which are access_key and number.
    Now, go to Restful Manager > Requests, add a new request. and give it a name, then select the connection name that you have just created. When the parameters show up, populate the parameter values as you see in the following screenshot.
    As you can see, there are a lot of options in Parameter values setting, most of them is to set the dynamic data retrieval which makes this plugin powerful:
    access_key: will be fixed value since access keys doesn’t change.
    number: the phone number would be a post/user meta as in this example. you can make it related to current logged in user by retrieving user’s meta phone number that the system is previously saved.
    Once you populated those fields, click save.
    paramvals
  3. Request’s Display Settings:
    After you save your values, you will find new sections are generated in the request page. One of them is ‘Display Settings’ which responsible for what you want to do with the response results:

    • Display Vs. Save then Display: WPRM give a chance to save selected items from the results in database or just display it in runtime. In this example, we will just choose ‘runtime request’ to only display the results.
    •  Where to display results: decide where to show the results template by selecting post type and then before or after content or you can just use a shortcode to add it wherever you like. We will use shortcode so we can add it as a widget(may be you want to add it in some user section).
    • Showing repeatable results: select where or not to loop through results. This used only when you have a group of results that share the same group of parameter keys(discussed in other example).
    • Start Checking dummy data: add all values in this section with full fixed(no dynamic work is here) dummy(but correct) data then click ‘check request’ once you see results check only the data that you want it to show up in your app or widget. once you’are finished click save.
      disset
  4. Templating Results:
    Now every thing is ready except how the results will look like!! so here comes the last section, ‘Template Settings’. You will find in it 2 parts:

    • Header or non-repeatable Template: this is the template that we’ll use. you can add text inline style and html to template you results. The links below the editor is a shorthand to help you copy and paste to the editor:
      • the results keys
      • Functional helpers like: if/ifExists/section

      In the GIF below, we check if the number is valid by checking the ‘valid’ results item if it’s equal to 1(check the documentation).

    • Repeatable template: if you have a repeatable data you will setup this template exactly as the above to be in for each repeated item at a time.
      newphonetemp
      Click to see full image and clear text

      Save the changes once you finish. The GIF above shows how it would look like. It’s so simple but it could be effective in real app or website.

WPRM Little Longer Example: relevant related posts from Google

This plugin enables you to manage RESTful APIs data starting from establishing a connection to templating the results. Here is a example puts WPRM in action.

In this example, we will return a  relevant related posts from your favorite site using Google Custom Search API. Please, make sure that:

  1. Establishing Connection:
    If you checked the API’s documentation, as we learnt before, you will need to use a URL which have 2 parts:

    • base url : https://www.googleapis.com/customsearch/v1
    • parameters : key(global Google api access key), cx(custom Search API Engine ID), q(search term), num(number of results).
    • Data format is JSON.
    • If your API are authorized with Email and password, so you can check the option of authorized connection and add username and password.
    • If you have any header extra data (sometimes api documentation need you to add authorization key in the header instead of the parameters), add as many as you like in the ‘Extra header data’ section.

    Once you now that, go to Restful Manager > Connection, add a new connection, give it a relevant name(you will need it soon)  and add the data as in the following screenshot then click Save.
    Note: You can get api key to the API and get Engine ID read the documentation and get the id from here. Also, you will be asked to add a website or a blog to retrieve your results from.
    conn

  2. Create request and Connection Settings:
    Next, we need to assign those parameters to suitable data so that the web service can query it and return the required data response.
    Now, go to Restful Manager > Requests, add a new request. and give it a name, then select the connection name that you have just created. When the parameters show up, populate the parameter values as you see in the following screenshot.
    As you can see, there are a lot of options in Parameter values setting, most of them is to set the dynamic data retrieval which makes this plugin powerful:
    key: will be fixed value since it doesn’t change.
    cx: the same, fixed value.
    q: this one will be dynamic. We want the results to be as relevant as possible so we will use the post title as the search term so now the results will be different once the post ch
    num: here i make it fixed so it only output 3 related posts.
    Once you populated those fields, click save.
    req
  3. Request Display Settings:
    After you save your values, you will find new sections are generated in the request page. One of them is ‘Display Settings’ which responsible for what you want to do with the response results:

    • Display Vs. Save then Display: WPRM give a chance to save selected items from the results in database or just display it in runtime. In this example, we will just choose ‘runtime request’ to just display the results.
    •  Where to display results: decide where to show the results template by selecting post type and then before or after content or you can just use a shortcode to add it wherever you like. We will show the related posts after the post content.
    • Showing repeatable results: select where or not to loop through results. This used only when you have a group of results that share the same group of parameter keys and that is the case here. All search results share the same param key so we will check this option.
    • Repeatable Results Root: Sometimes the part of the results that you need is not quite in the result’s top level but in some deeper level. To populate it, you need to run the initial request with dummy data first(check the results screenshot below).
      Note: the results will be look look this:
      items[
      Repeatable-0 [
      data …
      ],
      Repeatable-1 [
      data …
      ],
      repeatable-2 [
      data …
      ],
      ]
      while Repeatable was referred to as Sequence in older versions.
      display
    • Start Checking dummy data: add all values in this section with full fixed(no dynamic work is here) dummy(but correct) data then click ‘check request’ once you see results check only the data that you want it to show up. Here we will check the 4 main data: htmlTitle, link, htmlSnippet and src(for post thumbnail). Once you’are finished click save.results
  4. Templating Results:
    Now every thing is ready except how the results will look like!! so here comes the last section, ‘Template Settings’. You will find in it 2 parts:

    • Header or non-repeatable Template: this is the template that we’ll use. you can add text inline style and html to template you results. The links below the editor is a shorthand to help you copy and paste to the editor:
      • the results keys
      • Functional helpers like: if/ifExists/section
    • Repeatable template: This will template each repeated item at a time.
      temp
      Save the changes once you finish.

To see the output, open one of your posts scroll to after content section. you will find the results.
samples

WPRM Quick Launch with Example: currency exchange widget

WPRM aka WP RESTful APIs Manager is multi-purpose and can be generally used. It enables you to manage RESTful APIs data – whether it’s live currency exchange, weather forecasting data, google services data, or any other RESTful API – starting from establishing a connection to customizing templates for the results. Here is an example that puts WPRM in action.

Get WPRM Plugin

launch

In this example, we will return a currency exchange table against US dollar using Currency Layer API. Please, make sure that WP Restful Manager Plugin installed and active.

  1. Establishing Connection:
    If you checked the API’s documentation, you will find that to make a request to the service server you have to use a URL which have 2 parts: base url (http://apilayer.net/api/live) and parameters (access_key=YOUR_ACCESS_KEY& currencies=AUD,EUR,GBP,EGP). The first part is static and the second is dynamic in most cases which means that you can change the results if you changed the parameters’ values.
    To establish a connection you need to know 3 thing about your API:

    • Its base url.
    • the results (response) format [Json, XML, etc ..].
    • The parameters Keys: the name of the parameters like access_key & currencies.
    • If your API are authorized with Email and password, so you can check the option of authorized connection and add username and password.
    • If you have any header extra data (sometimes api documentation need you to add authorization key in the header instead of the parameters), add as many as you like in the ‘Extra header data’ section.

    Once you know that, go to Restful Manager > Connection, add a new connection, give it a relevant name(you will need it soon)  and add the data as in the following GIF then click Save.
    Note: You can get access key to the API by signing up for a free plan.

    rm-connection
    Click to show full image with clear text

    Authorization section
    Authorization section
  2. Create request and Connection Settings:
    Next, we need to assign those parameters to suitable data so that the web service can query it and return the required data response. In our example and for simplicity, we’ll only add one parameter value which is the access_key.
    Now, go to Restful Manager > Requests, add a new request. and give it a name, then select the connection name that you have just created. When the parameters show up, populate the parameter values as you see in the following GIF.
    As you can see, there are a lot of options in Parameter values setting, most of them are to set the dynamic data retrieval which is one of the features that makes this plugin powerful(we will discuss it more in other tutorial).
    Here we’ve chosen ‘fixed value’ since the access key is not dynamic.
    Once you populated those fields, click save.

    Click to show full
    Click to show full image with clear text
  3. Request’s Display Settings:
    After you save your values, you will find new sections are generated in the request page. One of them is ‘Display Settings’ which responsible for what you want to do with the response results:

    • Display Vs. Save then Display: WPRM give a chance to save selected items from the results in database or just display it in runtime. In this example, we will just choose ‘Display Only’ (‘runtime request’ in old versions) to only display the results.
    •  Where to display results: decide where to show the results template by selecting post type and then before or after content or you can just use a shortcode to add it wherever you like. Also, . We will use shortcode so we can add it as a widget.
    • Showing repeatable results: select where or not to loop through results. This used only when you have a group of results that share the same group of parameter keys(will discussed in other example).
    • Start Checking dummy data: add all values in this section with full fixed(no dynamic work is here) dummy(but correct) data then click ‘check request’ once you see results check only the data that you want it to show up in your app or widget.

      Click to show full
      Click to show full image with clear text
  4. Templating Results:
    Now every thing is ready except how the results will look like!! so here comes the last section, ‘Template Settings’. You will find in it 2 parts:

    • Header or non-repeatable Template: this is the template that we’ll use. you can add text inline style and html to template you results. The links below the editor is a shorthand to help you copy and paste to the editor:
      • the results keys
      • Functional helpers like: if/ifExists/section
    • Repeatable template: if you have a repeatable data you will setup this template exactly as the above to be in for each repeated item at a time.
      Click to show full
      Click to show full image with clear text

      Save the changes once you finish.

To see the output, we’ll copy  and paste the shortcode from the display section in any text widget in your theme.

The orange arrow refers to the output. Live currency exchange values.
The orange arrow refers to the output. Live currency exchange values.