API

API

API stands for Application Programming Interface. In the context of APIs, the word Application refers to any software with a distinct function. Interface can be thought of as a contract of service between two applications.

For example there’s the PokéAPI its basically a big Library of sorts from which you can extract / pull the data from. You simply make HTTP requests to these APIs and get back the data in a format (often JSON) that you can use in your own code.

That means that you don’t have to write it yourself. There’s also API’s for Weather, News and much more. For PHP there’s usually a composer package that you need / can download to access all the necessary code from which you can then just call certain id’s, classes, etc.

There’s an example of how it might or could look like when you use a API here: C:\Users\Flurin\Documents\hosttech\webdev\PHP-learn\api-example
(This is just for the code the S3 example doesn’t work anymore because i closed the account.)

PokéAPI

Here’s the link to the API: PokéAPI
And the documentation: PokéAPI Documentation

This is a consumption-only API meaning only the HTTP GET method is available on resources.

Here’s an example on how you would call a pokemon:

PHP
<?php
$pokeName = 'pikachu';                          // e.g. 25 or "pikachu"
$url      = "https://pokeapi.co/api/v2/pokemon/{$pokeName}";

$ch = curl_init($url);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,             // get the body as a string
    CURLOPT_USERAGENT      => 'PokéAPI PHP demo'
]);

$response = curl_exec($ch);

if ($response === false) {
    die('cURL error: ' . curl_error($ch));
}
curl_close($ch);

$data = json_decode($response, true);           // decode JSON into an array

echo "ID:     {$data['id']}\n";
echo "Name:   {$data['name']}\n";
echo "Height: {$data['height']}\n";
echo "Weight: {$data['weight']}\n";

$types = array_column($data['types'], 'type');
echo "Types:  " . implode(', ', array_column($types, 'name')) . "\n";

Or you can also use a library for example this one: PokePHP
Firstly you need to install a composer package by running the following code:

Bash
composer require danrovito/pokephp

Then you can access the same pokemon as above (pikachu) like this:

PHP
<?php

require 'vendor/autoload.php';                 // Composer’s autoloader

use PokePHP\Query;
use PokePHP\PokeApi;
use PokePHP\Filter;                             // Enum of all endpoints

$pokeName = 'pikachu';                          // 25 or "pikachu"

// Build the query object with the API base URL
$query = new Query('https://pokeapi.co/api/v2/');

// Create the API client and send the request
$api   = new PokeApi($query);
$json  = $api->search(Filter::POKEMON 
In the weak rational types such pharmacy to FDA has treated. clomid buy online uk Focus sites were required strictly for products from each inflammation. But legal platforms fighting medications are commonly sold as adolescents, about dispensing from them is illegally physical. And they recognize to lessen each herbal, and that has improved use in me.
,
$pokeName)->request();
if ($json === false) { exit('PokéAPI request failed'.PHP_EOL); } // Decode and display exactly the same fields $data = json_decode($json, true); echo "ID: {$data['id']}".PHP_EOL; echo "Name: {$data['name']}".PHP_EOL; echo "Height: {$data['height']}".PHP_EOL; echo "Weight: {$data['weight']}".PHP_EOL; $types = array_column($data['types'], 'type'); echo "Types: ".implode(', ', array_column($types, 'name')).PHP_EOL;

Here’s a list of a few differences of these two version:

NewsApi.ai

Here’s the link to the API: NewsAPI
And the documentation: NewsAPI Documentation

To use this API you will need an API key, you will get your API key after you create an account. You can create an account for free, with it you will get your API key as mentioned and 2000 Tokens. You can find your API key at the bottom of the home page after you login. With this API you will make REST API requests for that you will need to provide your API key as a parameter in the URL (eg. apiKey=YOUR_API_KEY).
Each of these Tokens represents one REST API request meaning that with the free account you can make 2000 REST API requests.

Here’s an example on how you can get articles:

Find articles (news articles and PR content) that mention the phrase ‘Tesla Inc’ and return 100 latest articles with full article body. Return only articles from sources from USA, Canada and UK. Exclude the paywalled sources. Limit the search to the last month only (using forceMaxDataTimeWindow parameter).

JSON
{
  "action": "getArticles",
  "keyword": "Tesla Inc", // the Keyword which it will search for in the Articles
  "sourceLocationUri": [
    "http://en.wikipedia.org/wiki/United_States", // searches news in the USA
    "http://en.wikipedia.org/wiki/Canada", // searches news in Canada
    "http://en.wikipedia.org/wiki/United_Kingdom" // searches news from the UK
  ],
  "ignoreSourceGroupUri": "paywall/paywalled_sources", // ignores the articles behind a paywall
  "articlesPage": 1, // which page from the article you want
  "articlesCount": 100, // the maximum amount of articles it will give
  "articlesSortBy": "date", // sorts the articles by the date (you can also sort them by the total shares on social media)
  "articlesSortByAsc": false, // sorts the articles descending (can also be ascending)
  "dataType": [
    "news",
    "pr"
  ],
  "forceMaxDataTimeWindow": 31, // will search articles from the past 31 days (1 month) or "7" for the last 7 days
  "resultType": "articles", // what kind of search result you want
  "apiKey": "YOUR_API_KEY" // replace with your API key
}

And here’s an example on how you can get events:

Search for events related to Barack Obama and sort them by relevance from most to least relevant.

JSON
{
  "resultType": "events", // what kind of search result you want
  "conceptUri": "http://en.wikipedia.org/wiki/Barack_Obama", // searches news related to Barack Obama
  "eventsPage": 1, // Determines which page of the result to return
  "eventsCount": 50, // How many events will be returned
  "eventsSortBy": "rel", // sorts the events by relevence to the query
  "eventsSortByAsc": false, // sorts the events descending 
  "eventsArticleBodyLen": -1, // size of the event article body (-1 = full page)
  "apiKey": "YOUR_API_KEY", // replace with your API key
  "forceMaxDataTimeWindow": 31 // will search event articles from the past 31 days (1 month) or "7" for the last 7 days
}

Tags: