Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
save Facebook test users to database
<?php
ini_set('precision', 20);
?>
<!-- Decreased Latency -->
<!-- Increased parallelism -->
<!-- Better caching -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="js/mustache.js" type="text/javascript"></script><!-- -->
<script>
var app = {
id : YOUR_APP_ID,
secret : YOUR_APP_SECRET
};
var fb = {
end_point: "https://graph.facebook.com",
api: function(uri, type, data, success_callback, error_callback){
if( data.batch!= undefined ){ // if making a batch call
data.batch = JSON.stringify(data.batch); // batch parameter must be a JSON array
}
$.ajax({
url: this.end_point+uri,
type: type,
data: data,
dataType: "json",
crossDomain: true,
success: function(response){
try{
success_callback(response);
} catch(e){ };
},
error: function(jqXHR, textStatus, errorThrown){
try{
error_callback(jqXHR, textStatus, errorThrown);
} catch(e){};
}
});
}
};
$(document).ready(function(){
var users = {}; // to store user information
var user_info = []; // array for mustache
var args = {
access_token: app.id + "|" + app.secret,
batch: []
};
args.batch.push({
method : "GET",
name : "get-users",
relative_url : "/"+app.id+"/accounts/test-users?limit=500", // by default the limit is 50, so get all 500
omit_response_on_success : false
});
args.batch.push({
method : "GET",
relative_url : "?ids={result=get-users:$.data.*.id}"
});
// make batch API call to get more information about this user. notice how we are submitting a POST to /
fb.api("", "POST", args, function(batch_response) { // success_callback
var body = JSON.parse(batch_response[0].body); // we need to get user's access token
$.each(body.data, function(i, user){
users[user.id] = user; // push to users object with id being the key
});
body = JSON.parse(batch_response[1].body);
$.each(body, function(id, user) {
$.each(user, function(k, v) { // get each peace
users[id][k] = v; // update users object with id being the key
});
user_info.push(users[id]); // push to user_info array
});
var block = "";
block += "{{#user_info}}";
block += "INSERT INTO fb_users (id, name, first_name, last_name, email, locale, access_token) VALUES ";
block += "('{{id}}', '{{name}}', '{{first_name}}', '{{last_name}}', '{{email}}', '{{locale}}', '{{access_token}}');<br><br>";
block += "{{/user_info}}";
$("#sql").html(Mustache.render(block, {"user_info": user_info})); // mustache
}, function(jqXHR, textStatus, errorThrown){ // error_callback
var response_text = JSON.parse(jqXHR.responseText);
$.each(response_text.error, function(k, v) {
console.log(k + " : " + v);
});
}); // end of fb.api call
});
</script>
<div id="sql"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment