这是我第一个使用facebookapi和facebookphp-sdk的项目,基本上我正在尝试获取所有用户状态。我写了一个应该可以工作的脚本,但我得到了500个错误(即使我更改了最大执行时间或设置了时间限制(0)),但只有当我在里面使用递归函数时,才可以查看代码:
$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个内部错误。正如我所说,我尝试更改最大执行时间或set_time_limit(0),但什么也没发生。我不确定问题是我的主机(godaddy),还是我的脚本不好/效率不高。有什么帮助吗?感谢Nico
我想我发现了你的问题。您正在将$nextResponse
转换为file_get_contents
返回的值。看见http://php.net/manual/es/function.file-get-contents.php在无法检索到内容的情况下返回CCD_ 3。尝试检查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.