PHP help - webservices

Soldato
Joined
1 Feb 2006
Posts
8,188
Hi guys,

I have a script which connects to a web service on the web and fills up some parameters which are then echoed out to some of my pages. Sometimes the service can be very unrealiable and so my webpage will only load half way then it times out while trying to connect to the soap service (it isn't my code).

I was thinking of running the web service call as a cron job and populating some fields with the result of the service call. I can then check on my page to see if the values are not empty then display them. This will save a call to the service and so won't break my page if it times out.

The thing i'm not sure about is how I will assign variables to store the result of the cron job. Say I run the script every night at midnight can I assign the results to some server variables which will still be accessible by every call to my webpage at any time in the next 24 hours until the script is ran again? Basically I just want to minimise the risk of letting a faulty web service connection prevent my pages from fully loading.

Just not sure what way is best to work this.

Thanks
 
Yes, you want to cache a successful response and only make another request to the web-service when the content is too old. Every other time you should use the cached data. Caching is something that's really important when using web-services - partly for performance, partly to avoid hammering the web-service and being rate limited.

When you come to refresh the cache data in your script, you should fall back on the old data if it fails but keep trying the lookup on subsequent requests until you retrieve another successful response. This way the lookup is nearly always transparent for users, except to the unlucky ones who happens to hit your site when the cache expires. I wouldn't bother with a cron-job, doing it this way means it's self-managing and you don't need to worry about the cron-job lookup failing and having to wait 24 hours for a retry.

The cached data could either be the XML file returned from the soap request or the result of your code parsing it and outputting some HTML or such. You can store this as a file on the file system or in a DB. Which method is best really depends on what data it is and how big, whether keeping it in XML is important and how high-volume your site is i.e. is file system access or DB access going to be more of a bottleneck.
 
Last edited:
Thanks for the info. It will be modified once a day so I got it working there with a db insert instead but the caching sounds better idea. It will only be a few lines of text to pull back each day as it is like a random generated text thingy. Will look into some caching tutorials and report back if i have problems. Thanks again.
 
Thanks again for the help Augmented. I have been doing php for my own wee projects for a few years and am surprised I never came across caching or how to use the output buffering. Both very handy but the looks of things. I created a gallery script a while back which iterates a directory and builds up links to images. This is done on every single call to the page so I guess caching the page would be a much faster option as the gallery contents wont change too often. The help is much appreciated.
 
Back
Top Bottom