Page 1 of 1

APIs

Posted: Thu Feb 24, 2011 12:00 am
by Torniquet
What would be the best way to develope a basic read only API to get user information to be displayed and used (from the database) on another website?

I have been looking for some decent AJAX tutorials to produce one (as i think they will probably be the safest) but there is no way of turning the returned values into php variable arrays / objects. (if there is i havnt found it)

The only sane solution i can find is php cURL. But this would mean that anyone wanting to use the API would need to have a cURL enabled host.

Is there any other ways i can do this?

The goal is to make an API in which a class script can be included and initialized, or somekind of source link in which a user can add their access key etc to in order to access it.

It really needs to be able to return a php array or object. Unless someone can suggest a way of doing it through ajax.

ta muchly

nighty night xx

Re: APIs

Posted: Thu Feb 24, 2011 12:09 am
by Jackolantern
So the key problem here is to get a value from AJAX into a PHP object? That would only require a simple variable assignment in the AJAX's called PHP script. However, I am thinking I am missing part of the issue here.

Re: APIs

Posted: Thu Feb 24, 2011 1:50 pm
by Xaleph
You are looking for JSONP. JSON is used mostly in the same domain environment, however, JSONP is used often for crossdomain.

Re: APIs

Posted: Thu Feb 24, 2011 3:09 pm
by hallsofvallhalla
My javascript tutorial uses a simple method to use ajax to retrieve PHP arrays and data. Just build from it. I hate JSON, makes no sense to me when it is soo simple to just use standard data and split it.

Re: APIs

Posted: Thu Feb 24, 2011 3:53 pm
by Xaleph
Why bother splitting data from PHP and later on, in your JS doing heavy string operations? A JSON object is almost the same as a JS object, in fact, there are few differences. It`s a big array with objects inside of it. You can loop through them as simple as for(obj in jsonObj){ /*do something */ }

Ohhwell, i`m not to judge anyway, i picked up JS last month lol. My Js knowledge in raw js is limited.

Re: APIs

Posted: Thu Feb 24, 2011 5:59 pm
by Torniquet
thanks halls ill look at your tuts and see what i can find :)

@Xaleph

I have never really used JS let alone JSON and never heard of JSONP lol. you got any links to simple tuts which show you how to use it effectivly?

thanks for the input guys :)

Re: APIs

Posted: Thu Feb 24, 2011 6:08 pm
by Xaleph

Re: APIs

Posted: Thu Feb 24, 2011 6:51 pm
by Chris
hallsofvallhalla wrote:I hate JSON
What, that's like saying "I hate JavaScript" all JSON is is an array in JavaScript.

An array:

Code: Select all

Array ( [foo] => bar )
The array in PHP:

Code: Select all

$array = array( 'foo' => 'bar' );
 
in JavaScript:

Code: Select all

var array = new Array( 'foo' => 'bar' );
or

Code: Select all

var array = { 'foo' : 'bar' }; // actually an object, array would be [ 'foo' : 'bar' ]
PHP seralized array:

Code: Select all

a:1:{s:3:"foo";s:3:"bar";}
What this is saying, array with one value key is a string with 3 characters: 'foo' and has the value of a string 3 with characters: 'bar'.

JSON

Code: Select all

{"foo":"bar"}
Wait, which one is the shortest here? I think it's JSON(JavaScript(ECMAScript)).

Re: APIs

Posted: Thu Feb 24, 2011 8:27 pm
by dave3460
i have sorted a api feed out .

i use the api address that i was given and then the key . and sort it in a sql database and then just show what info i require .

If this is what you are on about i can post how i did it .

if you want this side or was you after the create the api side so others can use the info?

Dave

Re: APIs

Posted: Thu Feb 24, 2011 9:33 pm
by Torniquet
well both really, but mainly the side the client will use to access my site to gather user information