php - Rookie questions about json ← (PHP, JQuery, MySQL, HTML)

I know this is a really rookie question but would like some understanding off where to start from guys in the know.

Say if i have a website called

www.example.com

and its a blog website with lots off blog posts.

I would like to setup the website so i can pull data from i via a http request. Take you tube for example.

http://gdata.youtube.com/feeds/api/videos?alt=json

if i paste this in the browser i get all the json which is great for me to use and pull from anywhere.

I would like to setup my website so that i can pull feeds in it from any other website through the http.

http://www.example.com/feeds/api/posts?alt=json

so i can then grab the json with jquery etc.

Can someone please give me a quick understanding off technologies that would be used for this.

Can i do this with php i have looked at curl does anyone now a good tutorial to get me rolling. would i be allowing a feed to my database for them to pull info from their? i am a bit confused to how i all work to be honest.

Any help from you guys would be great ;)

Answer



Solution:

You have two options.

API

This involves creating an interface to your application. Read http://www.gen-x-design.com/archives/create-a-rest-api-with-php/ for details.

Feed

You essentially create a read-only output of your data in a previously specified format. Read http://www.ibm.com/developerworks/library/x-phprss/ for details.

Answer



Solution:

Docs:
http://php.net/manual/en/book.json.php
http://www.php-mysql-tutorial.com/wikis/php-tutorial/reading-a-remote-file-using-php.aspx
http://is449w.blogspot.com/2010/03/parsing-json-response-with-php.html

In simply way: PHP has functions to work with JSON (encode and decode objects)

an array in json ["a","b","c"] will become an array in php {"a","b","c"} simply by calling

$myarray = json_decode("[\"a\",\"b\",\"c\"]")

Grabbing an remote JSON (API) output is thing discussed in second link i posted (reading a remote file using php), you have several options, where CURL is probably the most complex and file_get_contents most simply

usual steps are

  • Get address of remote feed/api (via url attribute or from database)
  • Grab the remote URL
  • Push contents of remote URL into View (html template file) or
    request it from server via jQuery ajax and work with it directly

Please note a rule about insecure cross-domain access
( Accessing web Service from jQuery - cross domain )

And second note is about implementing security authentication to your API/feed output, if you need it to work privately
Web API Security
PHP API Key Generator
API Security: how to restrict access by domain?
Having security in API System | PHP cuRL

Source