Using Data That Isn't Yours

162 views

Published on

Stubby shows you how to use super-duper basic web services.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
162
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Using Data That Isn't Yours

    1. 1. Using Data That Isn’t YoursHow to consume web services to augment your web sites and apps. An Adventure With Stubby
    2. 2. Stubby stubby@cogsprocket.net StubbyTheGreat
    3. 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. 4. Where Are They? EVERYWHERE. Twitter FacebookGoogle Calendar YouTube Dribbble Your Blog
    5. 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. 6. Know How To Get It•Find the service you want to consume• Read the documentation • https://dev.twitter.com/docs
    7. 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. 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. 9. The Anatamy Of A Requesthttp://api.twitter.com/1/statuses/user_timeline.json?screen_name=<screen_name> How To Make The Request $data = file_get_contents(<api_url>);
    10. 10. What You’ll Likely Get A StringYeah... You were probably hoping for something more exotic....
    11. 11. Bad news...A string of data is something, but it’s not anything super duper useful...
    12. 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()
    13. 13. What JSON Looks Like $results->created_at $results->text
    14. 14. What XML Looks Like $results->created_at $results->text
    15. 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. 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. 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. 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. 19. What Could Go Wrong? The service could be unavailable...
    20. 20. What Could Go Wrong?The remote end could block your access
    21. 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

    ×