Php 来自FB图形API的长TTFB

Php 来自FB图形API的长TTFB,php,facebook,http,facebook-graph-api,http-headers,Php,Facebook,Http,Facebook Graph Api,Http Headers,我编写了一个自制的PHP脚本,以便从页面中获取最后一篇FB帖子,当我的文档准备就绪时,只需从jQuery/AJAX代码执行: $(“我的对象”).load(“fb_feed.php”) 在本地PHP服务器上运行作为VPS上的,到第一个字节的时间非常长:10s至少,20s平均,或更长 ini_set("allow_url_fopen", 1); function getGraphUrl($param){ return "https://graph.facebook.com/". $par

我编写了一个自制的PHP脚本,以便从页面中获取最后一篇FB帖子,当我的文档准备就绪时,只需从jQuery/AJAX代码执行:
$(“我的对象”).load(“fb_feed.php”)

在本地PHP服务器上运行作为VPS上的,到第一个字节的时间非常长:10s至少,20s平均,或更长

ini_set("allow_url_fopen", 1);

function getGraphUrl($param){
    return "https://graph.facebook.com/". $param ."access_token=THE_TOKEN";
}
function getContent($param){
    $url = getGraphUrl($param);
    return json_decode(file_get_contents($url));
}
$feed = getContent("ID_OF_THE_PAGE/feed?");
$posts = array();

foreach ($feed->data as $i => $post) {
    if(isset($post->message)){
        $object_id = getContent($post->id . "?fields=object_id&")->object_id;
        if(isset($object_id)){
        $img_url = "https://graph.facebook.com/" . $object_id . "/picture";
        $short = (getimagesize($img_url)[1] < 170 ? true : false);
        if($short){
            $img_url = "images/empty.png";
        }
        array_push($posts, array('id' => $post->id,'message' => $post->message,
        'img_url' => $img_url, 'date' => $post->created_time));
        }
    }
    if(count($posts) == 6){
        break;
    }
}
ini\u集(“允许url\u fopen”,1);
函数getGraphHull($param){
返回“https://graph.facebook.com/“$param.”访问\u令牌=该\u令牌”;
}
函数getContent($param){
$url=getGraphUrl($param);
返回json_decode(文件_获取_内容($url));
}
$feed=getContent(“页面/提要的ID”);
$posts=array();
foreach($feed->data as$i=>$post){
if(设置($post->message)){
$object\u id=getContent($post->id。“?fields=object\u id&”)->object\u id;
if(isset($object_id)){
$img_url=”https://graph.facebook.com/“$object_id。”/picture”;
$short=(getimagesize($img_url)[1]<170?true:false);
若有(短){
$img_url=“images/empty.png”;
}
数组推送($posts,数组('id'=>$post->id,'message'=>$post->message,
'img_url'=>$img_url,'date'=>$post->created_time');
}
}
如果(计数($posts)=6){
打破
}
}

PHP脚本的摘录你说你在每一次页面加载中都在执行哪些加载帖子?然后是的,当然你应该在你的终端缓存数据;不仅是因为时间问题,还因为你可能很快就会遇到API速率限制。好的,在这种情况下,问题解决了,谢谢