Skip to content

Instantly share code, notes, and snippets.

@mojowen
Last active December 28, 2015 01:19
Show Gist options
  • Save mojowen/7419712 to your computer and use it in GitHub Desktop.
Save mojowen/7419712 to your computer and use it in GitHub Desktop.
An example of a PHP API Interface
<?php
$url = $_POST['url'];
$url_count = get_fb_info( $url );
echo json_encode( $url_count );
function get_fb_info( $site_name ) {
$query = "https://api.facebook.com/method/fql.query?query=select url,share_count,like_count,comment_count from link_stat where url = ";
$sites_query = '"'.$site_name.'"';
$xml = simplexml_load_file( $query.$sites_query );
$urls = Array();
foreach ($xml->link_stat as $link) {
$link->total = (int)$link->like_count + (int)$link->share_count + (int)$link->comment_count;
array_push($urls,$link);
}
return $urls[0];
}
?>
<html>
<body>
<form action="./fb_api_wrapper.php" method="POST">
<input type="text" name="url">
<input type="submit">
</form>
</body>
</html>
<html>
<script type="text/javascript" src="./tinyxhr.js"></script>
<script type="text/javascript">
function ajax_form( form ) {
tinyPost( // This is a function that will make the POST request with AJAX
form.action, // We're getting the URL of the form from the form's action attribute
{ url: url.value }, // This is the data
function(result) { print_result.innerText = result.url + ' has ' + result.share_count + ' shares'} // All AJAX requests are asynchronous - so they need a callback to do a follow up action when they're done
)
return false; // Return false to override the default form submission
}
</script>
<body>
<form action="./fb_api_wrapper.php" method="POST" onsubmit="return ajax_form(this);">
<input name="url" id="url">
<input type="submit">
</form>
<div id="print_result"></div>
</body>
</html>
/* Library for making AJAX requets */
function tinyPost(url,data,callback) {
tinyxhr(url,data,callback,'POST');
}
function tinyGET(url,data,callback) {
tinyxhr(url,data,callback);
}
function tinyxhr(url,data,callback,method,contenttype,timeout) {
var requestTimeout,xhr;
try{ xhr = new XMLHttpRequest();
} catch(e){
try{ xhr = new ActiveXObject("Msxml2.XMLHTTP");}
catch (e){ return null; }
}
requestTimeout = setTimeout( function() {
xhr.abort(); callback(new Error("tinyxhr: aborted by a timeout"), "",xhr);
}, timeout || 5000);
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
clearTimeout(requestTimeout);
var response = xhr.responseText
try { response = JSON.parse( response ); } catch( e ) {}
callback( response, (xhr.status != 200 ? new Error("tinyxhr: server respnse status is "+xhr.status) : false), xhr );
}
xhr.open( method ? method.toUpperCase() : "GET", url, true);
function serialize(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}
if(! data ) { xhr.send();
} else {
if( typeof data == 'object' ) data = serialize(data);
xhr.setRequestHeader('Content-type', contenttype ? contenttype : 'application/x-www-form-urlencoded');
xhr.send(data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment