I want to use some data from an API to create a page for my website. WordPress was used to create the website. I've tried a number of online codes and functions without luck. I must log in to the API using a user, pass, or token (I prefer token). Since I'm brand-new to PHP and API, I don't even know where to begin.
I want to use the Exoclick API to get data if anyone has used it before. I can see that the request is made with curl from the Exoclick API interface, and I would prefer not to utilize it. Here's an illustration:
curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer tokenhere' 'https://api.exoclick.com/v2/statistics/publisher/country?'
I discovered this login code in the API manual, however when I pasted it into a test page, it simply crashed my website:
<?php
// Include Request and Response classes
$url = 'https://api.exoclick.com/v2/login';
$params = array(
'api_token' => 'tokenhere'
);
// Create a new Request object
$request = new Request($url, 'POST', $params);
// Send the request
$request->send();
// Get the Response object
$response = $request->getResponse();
if($response->getStatusCode() == 200) {
// Retrieve the session token details
$token = $response->getBodyDecoded();
print_r($token);
}
else {
echo $response->getStatusCode() . PHP_EOL;
echo $response->getReasonPhrase() . PHP_EOL;
echo $response->getBody() . PHP_EOL;
}
?>