dazzathedrummer Posted March 29, 2011 Share Posted March 29, 2011 Hi, I'm developing an iPhone app that's going to need data from a MySQL database, it's just a simple list of my bands upcoming gigs. So I understand that I need to build an API - I've found some scripts here and there, I just need some advice as to 'what' I should build...JSON? XML? something else? I don't know anything about it and cant really find anything that will tell me. Hopefully, If I can get a good method going I will be able to use it for various other app ideas. Any suggestions are greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/232058-where-to-start-creating-an-api/ Share on other sites More sharing options...
ignace Posted March 29, 2011 Share Posted March 29, 2011 You could make this as simple as: http://service.domain.top/bands.php Returning whatever you fancy. Of course this will be very specific to your needs, so if you are planning on letting other people use this API you will probably have to add some formats like XML and JSON, or maybe even the good old CSV http://service.domain.top/bands.xml Would also be valid, updating it overnight with a CRON. Quote Link to comment https://forums.phpfreaks.com/topic/232058-where-to-start-creating-an-api/#findComment-1193797 Share on other sites More sharing options...
dazzathedrummer Posted March 30, 2011 Author Share Posted March 30, 2011 ah ok, so i literally save a php file in my API location that returns a query in whatever format I want to bring into the iPhone app (or whatever)? .....and if i want to set up an API key, is that literally a string that I validate against the API to allow access? Quote Link to comment https://forums.phpfreaks.com/topic/232058-where-to-start-creating-an-api/#findComment-1194188 Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 Yeah pretty much. You can pass your api key as a url parameter: http://service.domain.top/bands.php?apikey=xxxxxx In the question of xml vs json, from an efficiency standpoint you are better off using json as it is a much more terse format, and in mobile applications, it's highly advisable to send the data in the most compact form possible. Php has a number of functions or you can use the zend_json library. The best bet depends on the complexity of your json data, as well as how current a version of php you are using. When you return your results, you want to set the correct header. header('Content-type: application/json'); Quote Link to comment https://forums.phpfreaks.com/topic/232058-where-to-start-creating-an-api/#findComment-1194190 Share on other sites More sharing options...
dazzathedrummer Posted March 30, 2011 Author Share Posted March 30, 2011 wow thats really helped me - great information!! So, i've created an api that outputs the gig date and venue. Does this look like a format that can be read by the app (I don't know what format i'm aiming for)..... [{"2011-03-31 23:59:00":"O'Neill's , Peterborough"},{"2011-04-29 23:59:00":"O'Neill's, Peterborough"},{"2011-04-30 23:59:00":"The Cock Inn , Peterborough"},{"2011-05-06 23:59:00":"O'neills, Southend"},{"2011-05-19 23:59:00":"O'Neill's, Peterborough"},{"2011-05-21 23:59:00":"The Old Bridge Hotel, Huntingdon"},{"2011-05-29 23:59:00":"The Bell, Sawtry"},{"2011-06-09 23:59:00":"O'Neill's, Peterborough"},{"2011-06-18 23:59:00":"O'Neill's, Peterborough"},{"2011-07-07 23:59:00":"O'Neill's, Peterborough"},{"2011-07-09 23:59:00":"O'neills, Southend"},{"2011-07-15 23:59:00":"The Golden Fleece, Stamford"},{"2011-07-29 23:59:00":"O'Neill's, Peterborough"},{"2011-08-11 23:59:00":"O'Neill's, Peterborough"},{"2011-08-13 23:59:00":"Ebenezers, Peterborough"},{"2011-08-27 23:59:00":"O'Neill's, Peterborough"},{"2011-08-30 23:59:00":"The Old Coach House, Deeping"},{"2011-09-02 23:59:00":"Parkway Club, P'boro"},{"2011-09-15 23:59:00":"O'Neill's , Peterborough"},{"2011-09-17 23:59:00":"O'neills, Southend"},{"2011-09-30 23:59:00":"O'Neill's, Peterborough"},{"2011-10-06 23:59:00":"O'Neill's , Peterborough"},{"2011-10-15 23:59:00":"The Old Coach House, Deeping"},{"2011-10-28 23:59:00":"O'Neill's, Peterborough"},{"2011-11-10 23:59:00":"O'Neill's, Peterborough"},{"2011-11-12 23:59:00":"O'neills, Southend"},{"2011-11-26 23:59:00":"O'Neill's, Peterborough"},{"2011-12-08 23:59:00":"O'Neill's Duo, Peterborough"},{"2011-12-17 23:59:00":"The Golden Fleece, Stamford"},{"2011-12-23 23:59:00":"O'Neill's, Peterborough"},{"2011-12-24 23:59:00":"O'Neill's, Southend"}] Quote Link to comment https://forums.phpfreaks.com/topic/232058-where-to-start-creating-an-api/#findComment-1194398 Share on other sites More sharing options...
dazzathedrummer Posted March 30, 2011 Author Share Posted March 30, 2011 probably easier to see from this link... www.api.the-guards.co.uk/jsontest.php?apikey=123456 Quote Link to comment https://forums.phpfreaks.com/topic/232058-where-to-start-creating-an-api/#findComment-1194402 Share on other sites More sharing options...
ignace Posted March 30, 2011 Share Posted March 30, 2011 You can validate your JSON output on: http://www.jsonlint.com/ You can also include JSON-P, which allows you to: http://service.domain.top/bands.php?apikey=XXX&callback=jsFunction Which would return the JSON wrapped in the passed callback (P in JSON-P, or padding) jsFunction(/*JSON code here*/); Quote Link to comment https://forums.phpfreaks.com/topic/232058-where-to-start-creating-an-api/#findComment-1194417 Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 On top of Ignace's great advice, there's this article which should help you out: http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/ Quote Link to comment https://forums.phpfreaks.com/topic/232058-where-to-start-creating-an-api/#findComment-1194482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.