2011-12-20 84 views
0

直到現在我正在使用下面的代碼,不幸的是它不再工作。請有人能幫助我嗎?我真的是初學者,所以我會很感激整個代碼或者我應該改變的。如何在PHP中獲取ID請求,ID用戶和ID用戶? 非常感謝您請求對話框 - 在PHP中處理

<script> 
     FB.init({ 
     appId : '111111111111111', 
     status : true, 
     cookie : true, 
     oauth: true 
     });  
     function sendRequestViaMultiFriendSelector() { 
     FB.ui({method: 'apprequests', 
      message: 'My Great Request' 
     }, requestCallback); 
     } 

     function requestCallback(response) { 
     $.post("process_ids.php", {uid: <?php echo $uid; ?>, request_ids: String(response.request_ids) }); 
     return false; 
     } 
    </script> 

///////////////////////process_ids.php : 

<?php 
    $appid ='11111111111111'; 
    $secret ='22222222222222222'; 

if(isset($_POST['request_ids']) && !empty($_POST['request_ids'])){ 

$app_token = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id='.$appid.'&client_secret='.$secret.'&grant_type=client_credentials'); //Get application token 

$sent = explode(',', $_POST['request_ids']); //Convert csv to array 
$count = count($sent); //count how many objects in array  
for ($a = 0; $a < $count; $a++) {  
$request = file_get_contents('https://graph.facebook.com/'.$sent[$a].'?'.$app_token); 

preg_match("/\"to\":\{\"name\":\"(.*?)\",\"id\":\"(.*?)\"/i", $request, $getInfo); 

$sent_to_name = $getInfo[1]; 
$sent_to_id = $getInfo[2]; 

preg_match("/\"id\":\"(.*?)\"/i", $request, $getInfo); 
$idrequest = $getInfo[1]; 

$delete_url = "https://graph.facebook.com/" . $idrequest . "?" . $app_token . "&method=delete"; 
} 
} ?> 

回答

0

的答案FB被傳遞給你的JSON編碼。而不是寫正則表達式的,你可以簡單地被儲存在$ request變量和json_decode答案()吧...

像這樣:

$request = file_get_contents('https://graph.facebook.com/'.$sent[$a].'?'.$app_token); 
$answer = json_decode($request); 
// that's for debugging only 
echo '<pre>'; 
print_r($answer); 
echo '<pre>'; 

這將輸出你已解碼的請求..那麼。你可以通過$答案 - > some_property

訪問conents如果你會使用PHP-SDK從GitHub它會更容易(例如刪除apprequests):

$requests=$this->facebook->api('/me/apprequests/'); //Requests Graph API call. 
foreach($requests['data'] as $request) { 
    $this->facebook->api($request['id'], 'delete'); // delete the used request 
}