I wanted to toss out a quick snippet here to help people get started using this new dialog.  I’ve been spending the last couple days implementing it in all my and my clients apps that use this type of interface. So below is a simple way to open the dialog and also handle the dialog once requests sent or if they cancel the request.

 

<a href="javascript:void();" onclick="sendRequest()">Send Application Request</a>
<script>
var request = {
method: 
'apprequests',
message: 
'You have received something very special.',
data: 
'some value to track like ID value of a item stored in database or key value',
title: 
'Select some of your friends'
};
function sendRequest() {
FB.ui(request, 
function (response) {
if (response &amp;&amp; response.request_ids) {
/* Do something after the user sends requests IE show a fancy graphic or record data in db */
else {
/* Do nothing or something whatever blows your hair back if they click cancel */
}
})
}
</script>

The above is just split apart so you can easily see whats in the request

Now if you want to combine them into a neat function.  We can do that and make it easy to send, say gifts to friends.  We all love jquery so we’ll use that here to easily do this for multiple items.

Ok say we have a database with all kinds of items and images for sending gifts to friends.  We would use a loop to get all this information loaded into a fancy layout.  Just an example below of 3 images pulled from a database.  Then using jquery to dynamically pull the id of said image from the id attribute of the image when a user clicks the image in your app, and opens the request dialog with that id in the data variable for later use.

 

<img src="imgsrc1.jpg" class="itemimg" id="1">
<img src="imgsrc2.jpg" class="itemimg" id="2">
<img src="imgsrc3.jpg" class="itemimg" id="3">
<script>
function sendRequest(keyid) {
FB.ui({method: 
'apprequests',
message: 
'You have received a pretty image from a friend.',
data: 
'keyid:'+keyid+'',
title: 
'Select friends to send your image to.'}, function (response) {
if (response &amp;&amp; response.request_ids) {
//Do something if they send it
else {
//Do something if they don't send it.
}
})
}
$(
".itemimg").click(function(){
keyid 
= this.id;
sendRequest(keyid);
});
</script>

Then create a response.php file and add the following code to “catch” all the requests:

$reqId = $_GET['request_ids']; // Get the id of the current request

$requests 
= $facebook->api('/me/apprequests/?request_ids='.$reqId);  //Get the request. Not sure if this is correct for specific request

$itemData 
= $requests[data][0][data];  //Get the data that was originally sent in the request

//Some code here to do whatever with the data

$delete_url 
= "https://graph.facebook.com/".$reqId."?access_token=".$token."&amp;method=delete";

$result 
= file_get_contents($delete_url);  //Delete the request so there is only one there next time.
 
 
摘自:http://www.howtobe.pro/facebook-php-sdk-the-new-facebook-request-dialog-guide
 
 
 
posted on 2011-09-19 18:18  小炒花生米  阅读(553)  评论(0编辑  收藏  举报