I'm trying to duplicate a public facebook page timeline on website, using the php SDK. Script works fine except for the execution time that is pretty long.
Searching around i've read that facebook API can be pretty slow sometimes, by the way i'd like some help figuring out if my script is fine or if some improvement can be done for a faster execution time.
Here's my simplified code :
require_once 'Facebook/autoload.php';
use Facebook\FacebookRequest;
$fb = new Facebook\Facebook([
'app_id' => 'myappid',
'app_secret' => 'appsecret',
'default_graph_version' => 'v2.8',
]);
$request = '/11239244970/posts?access_token=my_acces_token_';
$search = $fb->get($request);
$raw= $search->getGraphEdge();
$data = $raw->asArray();
$results = [];
foreach($data as $el){
if(isset($el['message'])){
try{
$messaggio = $el['message'];
$creato = $el['created_time'];
$newDate = $creato->format('d/m/Y');
$laData = str_replace("/","-",$newDate);
$id = $el['id'];
$evento = $el['story'];
$atch = $id.'/attachments?access_token=my_acces_token_';
$xx = $fb->get($atch);
$yy= $xx->getGraphEdge();
$atachment = $yy->asArray();
$like = $id.'/likes?summary=true&access_token=my_acces_token_';
$likes = $fb->get($like);
$likes = $likes->getGraphEdge();
$like_count = $likes->getTotalCount();
$atachments = [];
}
catch(Exception $e) {
}
//$likes = $likes->asArray();
foreach($atachment as $at){
if(isset($at['subattachments'])){
$atchs = $at['subattachments'];
foreach($atchs as $atx){
$atachments[] = $atx['media']['image']['src'];
}
}else if(isset($at['media']['image'])){
$atachments[] = $at['media']['image']['src'];
}
}
$temp = [$messaggio,$laData,$evento,$like_count,$atachments];
$results[]= $temp;
}
}
print_r($results);
That piece of code grabs the last (i think by default) 25 posts and check if they have message and atachments.
As i said this code works fine, it is getting what i need except for execution time that is about 5 to 10 seconds. Any suggestions or improvement? Thanks