What's the best way to use the Twitter API via PHP?

前端 未结 6 1956
梦如初夏
梦如初夏 2021-01-12 16:58

A client would like me to add their Twitter stream to their website homepage, using a custom solution built in PHP.

The Twitter API obviously has a limited number of

6条回答
  •  感动是毒
    2021-01-12 17:38

    My suggestion: Create a small simple object to hold the cache date and an array of tweets. Every time someone visits the page, it performs the following logic:

    A) Does file exist?

    Yes: Read it into a variable No: Proceed to step D)

    B) Unserialize the variable (The PHP pair serialize()/unserialize() would do just fine)

    C) Compare the age of the cache stored with the current time (a Unix timestamp would do it) Its over 5 minutes from each other:

    D) Get the newest tweets from Twitter, update the object, serialize it and write in the cache again. Store the newest tweets for printing, too. Its not: Just read the tweets from the cache.

    E) Print the tweets

    Simplest and easiest way to serialize the object is the serialize()/unserialize() pair. If you're not willing to put off the effort to make the object, you could just use 2D array, serialize() will work just fine. Give a look on http://php.net/serialize

    Considering you have no cPanel access, its the best solution since you won't have access to PEAR packages, cron or any other simpler solutions.

提交回复
热议问题