简体   繁体   中英

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?

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.

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM