funguy
funguy

Reputation: 2160

How to save json info to an array every 3 hours with php?

I have an access to an API under this link:

https://api.adform.com/Services/Security/Login?username=api_user&password=mypassword&callback=JsonPCallBack

It gets me back a ticket id in such manner:

JsonPCallBack({
  "Ticket": "GEZDIMZRHFTDEZRYGVSTEMJNGM2DMLKMNGRRWGMZNHA4DIMRNGE4DAZRTMVRTKMJXMUZSYTKXLEZEETSQJQ3DINKBGRDTMMSHJFBUMQKSGNLDKM2PKFFVITCG"
})

The issue is that every 3 hours it expires.

How would you make with php so that the script would regenerate itself evey 3 hours and save the ticket into:

$config = array(
    "ticket" => "GEZDIMZRHFRWGMZQGJSWKZRNGYYTCOLDMDNRSWCZRNMIYTIMJNMNSWGZTBHA2DGYLBGI4CYN2CJNBDIU2KIVBUEWSUKZHTOSKBIRKUYU2EIVLFUR2WKRBFKWCI"
);

Thanks!

Upvotes: 2

Views: 90

Answers (1)

Grokify
Grokify

Reputation: 16334

You can do the following which is very similar to managing OAuth access token refresh:

  1. Whenever you retrieve a SOAP authentication ticket, calculate the expiration time by adding 3 hours (or slightly less) to the current time.
  2. Save the expiration time along with the SOAP authentication ticket.
  3. Before each subsequent request, compare the current time with the expiration time. If the current time is past the authentication ticket expiration time, retrieve another SOAP authentication ticket and save it with the new expiration time before making the request.
  4. In case you make a request and receive an error, try to re-authenticate, get a new authentication ticket and make the request again.

This approach has the follow benefits:

  1. Only retrieve a new authentication ticket when you think you need it.
  2. Automatic refresh in case your ticket has expired or is otherwise invalidated.
  3. No need to run an external process.

Upvotes: 2

Related Questions