How to GET data from API and show it on PHP page

0 votes

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;
        }
    ?>
Jul 31, 2022 in PHP by Kithuzzz
• 38,000 points
12,035 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
1 answer

How to call function of one php file from another php file and pass parameters to it?

Hello @kartik, Include the first file into the ...READ MORE

answered Aug 27, 2020 in PHP by Niroj
• 82,840 points
26,162 views
0 votes
0 answers

How to extract and access data from JSON with PHP?

This is intended to be a general ...READ MORE

Jul 22, 2022 in PHP by narikkadan
• 63,600 points
878 views
0 votes
0 answers

PHP using extract() to pass array values as variables and display it on a page

In order to include a specific file ...READ MORE

Aug 6, 2022 in PHP by Kithuzzz
• 38,000 points
1,316 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,357 views
0 votes
1 answer

How to get thumbnail of youtube video from youtube API?

Hii @kartik, If you want the biggest image ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,840 points
1,647 views
0 votes
2 answers

How to override trait function and call it from the overridden function?

instead of: return traitcalc($v); use this: ...READ MORE

answered Dec 13, 2020 in PHP by Uncle Nick
3,645 views
0 votes
2 answers

Using PHP to get all file names in a folder stored in HDFS

So i found a workaround for the ...READ MORE

answered Mar 13, 2019 in Big Data Hadoop by Bhavish
• 370 points

edited Mar 13, 2019 by Omkar 4,386 views
0 votes
1 answer

The uploaded file exceeds the upload_max_filesize directive in php.ini error while uploading plugin

After looking at your ERROR, it seems ...READ MORE

answered Feb 8, 2022 in Others by Soham
• 9,710 points
686 views
0 votes
0 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP