2014-10-08 65 views
0

這是我第一個使用facebook api和facebook PHP sdk的項目,基本上我試圖獲得所有的用戶狀態。我寫了一個應該可以工作的腳本,但是我得到了一個500錯誤(即使我改變了最大執行時間或者設置了時間限制(0)),但是隻有當我使用遞歸函數時,才能看到代碼:Facebook api,遞歸函數和500錯誤

$request = new FacebookRequest(
$session, 
'GET', 
'/me/statuses' 
); 
$response = $request->execute(); 
$graphObject = $response->getGraphObject(); 
$x = $graphObject->getProperty('data'); 
$y = $x->asArray(); //now i got an array 
$paging = $graphObject->getProperty('paging'); // i pick paging with "next" and "prevoiuos " 
$paged = $paging->asArray(); //as array 
$counter = 0 ;    
foreach ($y as $el){ 
     echo ('<h3>'.$y[$counter]->message.'</h3>'); 
     echo "<br/>"; 
     echo "<br/>"; 
     $counter++; 
} 

$next = $paged['next']; // now i got url for load 20 more statuses 
$response = file_get_contents($next); // get content of url 

//recoursive function every time i use looper with content of next 
function looper($response){ 
    $array = json_decode($response, true); 
    $secondarray = ($array['data']); 
    $paging = ($array['paging']); // again i pick url for load next statuses 
    $next = $paging['next'];// again i pick url for load next statuses 
    $nextResponse = file_get_contents($next);// again i pick url for load next  statuses and i will use this. 
    $counter2 = 0 ;    
     foreach ($secondarray as $el){ // put on page next 20 statuses 

       echo ('<h3>'. $secondarray[$counter2]['message'] .'</h3>'); 
       echo "<br/>"; 
       echo "<br/>"; 
       $counter2++; 


     } 
     if (is_null($nextResponse) == false){ // if in next call i got 20 more statuses(not empty) call again this function 
      looper($nextResponse); 
     } else { echo "last message" ; die();} //else stop. 

} 

looper($response); 

}

如果我不記得的功能(基本上我註釋掉if語句)腳本工作正常,並打印出20點+ 20的狀態,否則給我500內部錯誤。 正如我所說我嘗試changin最大執行時間或set_time_limit(0),但沒有任何反應。 我不確定問題是我的主機(godaddy),還是我的腳本不好/不高效。任何幫助? 謝謝妮可

+0

必須是無限遞歸你應該看到$ nextResponse的實際值時,有沒有下一個響應......像你希望它是 – 2014-10-08 15:14:23

+0

它可能不能爲null你有什麼消息在php error_log嗎? – angeldelrio 2014-10-08 16:14:18

+0

no messages @angeldelrio – 2014-10-08 16:17:01

回答

0

我想我找到了你的問題。您正在設定$nextResponsefile_get_contents返回的值。請參閱http://php.net/manual/es/function.file-get-contents.php,如果內容無法檢索,則返回false。建議您檢查您的false代替null

 .......... 

     if (false != $nextResponse){ // if in next call i got 20 more statuses(not empty) call again this function 
      looper($nextResponse); 
     } else { echo "last message" ; die();} //else stop. 
+0

謝謝你!這是問題! – 2014-10-08 16:59:18

+0

順便說一句,我用if語句「$ nextResponse!= false」,因爲用==,他再次調用函數,即使沒有數據。 – 2014-10-08 17:03:39

+0

哎喲,我的壞,我糾正它 – angeldelrio 2014-10-08 17:13:33