PHP include Page.php, if Page=404 { PHP include API { WRITE API as Page.php (Storing APIs on Servers)

56 views Asked by At

I'm scraping data from an API. The problem is that I'm querying the data too often for each pageload, so I'd like to store the data on the server after the first query. This should be fine according to the TOS.

On example.com/page:

<?php include 'example.com/data'; ?>

On example.com/data:

<?php include 'api.com/include'; ?>

So I am including a page from my server, and that page is including the api data from the external server.

Question 1: How can I tell example.com/data to WRITE or save the information from api.com/include as a file on the server such as example.com/data1.php ?

Question 2: How can I tell example.com/page to php include example.com/data1.php, and if it's a 404 (doesn't exist) to include example.com/data instead?

With both questions 1 and 2 answered I can query the api once, store the data as a file, and if that file exists use the data from that page rather than have to query the api each time the page is loaded.

If you know of a better way of doing this I'd be grateful to learn it. Though it is important that I include from example.com/data from example.com/page rather than directly from api.com/include because example.com/data has the correct code handlers to properly interpret and filter the information from api/include.

If you can answer either of the two questions it would be a great starting ground for me solving the other problem.

Thank you!

1

There are 1 answers

1
Michael Ben-Nes On BEST ANSWER

You should use a class to decouple this behaviour. Something like this:

<?php

class ReadApiFromDomainDotCom
{

    const TTL = 3600; // File is fresh for 3600 sec
    const FILE_NAME = 'whatever.txt';
    const API_ADDRESS = "http://api.whatever.com";

    /**
     * @return string
     */
    public function get()
    {
        if ($this->isCacheValid()) {
            return file_get_contents(self::FILE_NAME);
        }
        return $this->readApi();

    }

    /**
     * @return bool
     */
    private function isCacheValid()
    {
        if (!file_exists(self::FILE_NAME)) {
            return false;
        }
        if ($this->isExpired()) {
            return false;
        }
        return true;
    }

    /**
     * @return bool
     */
    private function isExpired()
    {
        return time() - filemtime(self::FILE_NAME) > self::TTL;
    }

    /**
     * @return string
     */
    private function readApi()
    {
        $data = file_get_contents(self::API_ADDRESS);
        file_put_contents(self::FILE_NAME, $data);
        return $data;
    }
}

Then it will be easy as:

$apiReader = new ReadApiFromDomainDotCom();
$data $apiReader->get();

Haven't tested this code so you will have to fiddle with it a bit. Add namespace, paths and so on.