user1044220
user1044220

Reputation: 371

how to make an api for my website

Im creating a review website, and one of the features is that you can get an API that shows your 5 star (*) rating from the site on another site.

I was wondering what is the best way to go about this?

I thought of just using an iframe but that didn't seem like the best solution.

I was hoping to do it through javascript someway like facebook and twitter, any ideas?

Upvotes: 1

Views: 4552

Answers (3)

Tanoro
Tanoro

Reputation: 869

Some Facebook widgets do use iframes. Twitter uses javascript that most likely interacts with Ajax scripts on their server and returns HTML code to output to an HTML element that is part of their widget embed code. Either way works.

Example based on Javascript and PHP (no Ajax):

Embed Code

<script type="text/javascript">

width = 527;
height = 95;

</script>
<script type="text/javascript" src="http://www.mysite.com/widget.js"></script>

widget.js

document.write('<iframe scrolling="no" frameborder="0" width="' + width + '" height="' + height + '" src="http://www.mysite.com/showdata.php?height=' + height + '&width=' + width + '" style="border: 1px solid #6c82b5;"></iframe>');

And showdata.php could be any PHP script you want to display any data you want. You can give your users some flexibility about design and colors by passing variables through the javascript as you see me doing here.

Upvotes: 2

Mathieu Dumoulin
Mathieu Dumoulin

Reputation: 12244

Using a dynamic image is a bit better.

//Url would be something like: http://mysite/api/userrating.php?id=userchecked
$user = mysql_fetch_assoc(mysql_query('SELECT * FROM users WHERE username = "'.mysql_real_escape_string($_GET['id']).'"'));

//Return a header to the caller that points towards the right 5 star image
header('location: http://mysite/api/userrating'.$user['rating'].'.jpg');

Obviously, this example is not very secure, lots more effort to put into it, but it gives you a general idea...

Upvotes: 0

Jens
Jens

Reputation: 1315

You may create a PHP-based function library or class, like the one Facebook offers. They provide developers with a class that is able to communicate with the Facebook servers via a given user's credentials; And via this pull profile information, gallery, friend lists, etc.. It is authorized via the Facebook servers, as well. You may get a lot of inspiration from there - even source code (it's provided, but I'm not sure it's actually open source). Of course building such a huge API to show a rating is not necessary, but if you wanted to learn to code an API "the right way", I'd say that's a good place to find inspiration.

https://developers.facebook.com/docs/reference/php/

Another thing is that you can pull information via HTTP both with a server-side script, or with an AJAX script that runs a server-side script after load of the general website. You may want to choose the later, because pulling HTTP information will slow down the execution of the entire script.

Upvotes: 1

Related Questions