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

I want to use some information from an API to create a page for my website. WordPress become used to create the website. i’ve tried a number of on-line codes and features without good fortune. I need to log in to the API the usage of a person, skip, or token (I opt for token). due to the fact that i am brand-new to Hypertext Preprocessor and API, I don’t even understand where to start.

<?php
    function get_data_from_api() {
        // Initialize cURL session
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://api.example.com/data_endpoint",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
        ));

        // Execute cURL session and get the response
        $response = curl_exec($curl);

        // Check for cURL errors
        if ($response === false) {
            echo "cURL Error: " . curl_error($curl);
            return;
        }

        // Close cURL session
        curl_close($curl);

        // Decode the JSON response
        $data = json_decode($response, true);

        // Check if decoding was successful
        if ($data === null) {
            echo "JSON Decode Error: " . json_last_error_msg();
            return;
        }
    ?>

        <?php foreach($data['data'] as $dataItem){ ?>
            <?phpif (isset($dataItem['id'])){ ?>
                    <h2><?= $dataItem['Posting_Title']; ?></h2>
                </div>
            <?php }else { ?>
            <p class="card-title">ID Not Found</p>
        <?php }  ?>
    <?php
    }
}
// Run the initial function
 get_data_from_api();
?>
Learn Tutorials Point
We Well organized and easy-to-understand Interactive tutorials With lots of examples of how to use Tutorials WordPress, PHP, Bootstrap, Bootstrap 4, HTML, CSS, AJAX, jQuery, Woocommerce, Post, Database, Javascript, Theme, Responsive, Templates.

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact us

Fill in the form below or give us a call and we'll contact you. We endeavour to answer all enquiries within 24 hours on business days.