3. What’s a Web Service
A web service is a component in a web application
that allows the owner to share data with remote applications.
It’s like a drive-thru library...
“If you want my books I’ll give you a chute where you can pick them up.”
4. Where Are They?
EVERYWHERE.
Twitter Facebook
Google Calendar YouTube
Dribbble Your Blog
5. Different Kinds of Services
• REST - Representational State Transfer
• Ruby on Rails does this by design
• SOAP - Simple Object Access Protocol
• Native to .NET, not well supported in other platforms
• RSS - Really Simple Syndication
• XML service that power blog subs and podcasts
6. Know How To Get It
•Find the service you want to consume
• Read the documentation
• https://dev.twitter.com/docs
7. Things To Look Out For
• Services with poor or no documentation
• Request rate limits
• Data return formats
• Service types not supported by your language
8. The Basics...
•Check the API for the service you want to consume
• Pull the data into your application
• Iterate through the data returned
• Output or store the data
• Super duper easy, right?
9. The Anatamy Of A Request
http://api.twitter.com/1/statuses/user_timeline.json?screen_name=<screen_name>
How To Make The Request
$data = file_get_contents(<api_url>);
10. What You’ll Likely Get
A String
Yeah... You were probably hoping
for something more exotic....
11. Bad news...
A string of data is something, but it’s
not anything super duper useful...
12. What You’ll Likely Get
Here’s a problem... Let’s fix it with objects.
•JSON - Javascript Object Notation
• Make the data readable with json_decode()
• XML - Extensible Markup Language
• Make the data readable with simplexml_load_string()
15. What’s Next?!
Now that you’ve got an object you can totally iterate through it.
// Super-scary iteration
for($i=0; $i <= 15; $i++) {
// Do something awesome
}
16. “for()” Can Be Scary
You can totally use foreach()
// Less-than-scary iteration
foreach($results as $result) {
// Do something awesome
}
foreach() will iterate through everything
17. How I Like To Do it...
You can totally use foreach()
// Madman iteration
foreach($results as $result) {
$this->{$value} = $result->...
}
Makes $object->VariableName... (‘Cause I treat code like objects)
18. What To Do With The Data
• Output it!
• You got it for a reason, give it a good ol’ “echo” and let the world see
• Store it!
• Probably a good idea to store any data for at least 5 minutes...
19. What Could Go Wrong?
The service could
be unavailable...
20. What Could Go Wrong?
The remote end could
block your access
21. Be Like Stubby
Behave yourself...
• Cache your data
• Limit the number of requests you make per hour
• Use a local iterator
• Local iterators duplicate the service and handle caching