dongwen7187 2013-12-15 13:15
浏览 33
已采纳

Facebook api - 由php发布非常缓慢

On my site users can share theirs reflinks by button. Problem is very slow posting on facebook. My code is following:

if (isset($_GET['error_reason']) && $_GET['error_reason'] == 'user_denied') 
{
    echo 'You must accept permissions!';
} 
else 
{
    include_once 'src/facebook.php';

    $appId = '.............';
    $secret = '............';
    $returnurl = 'http://site.pl/user/postOnFb.html'; 
    $permissions = 'publish_stream';

    //clean cache
    $_SESSION['fb_'.$appId.'_user_id'] = '';
    $_SESSION['fb_'.$appId.'_access_token'] = '';

    $facebook = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
    $fbuid = $facebook->getUser();

    if ($fbuid) 
    {
        try 
        {
            $status = $facebook->api('/me/feed', 'POST', array('message' => 'TEST'));
            if($status)
            {
                echo 'Post was succesfully published';
            }
        } 
        catch (Exception $e) 
        {
            echo 'Error! Try again later.';
        }
    }
    else
    {
        $fbloginurl = $facebook->getLoginUrl(array('redirect_uri'=>$returnurl, 'scope'=>$permissions));
         header('Location: '.$fbloginurl);
    }
}

I debugged code and '$facebook->api()' takes about 7 sec. Facebook api is so slow? Or what is wrong?

Edit: I was debugging and I noticed function makeRequest in BaseFacebook is slow:

var_dump(time()); // i add this
$result = curl_exec($ch);
var_dump(time().'</br>'); // i add this

Output:

int(1387198153) int(1387198158) //5 seconds
int(1387198158) int(1387198163) //5 seconds
int(1387198164) int(1387198171) //7 seconds

So problem is with curl_exec(), but what is wrong?

  • 写回答

1条回答 默认 最新

  • douti0687 2014-01-01 12:37
    关注

    There are something problems with protocol IPv6 which Facebook use. The solve is set curl to use only ipv4.

    base_facebook.php:

      public static $CURL_OPTS = array(
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 60,
        CURLOPT_USERAGENT      => 'facebook-php-3.2',
        CURLOPT_IPRESOLVE      => CURL_IPRESOLVE_V4, // <-- ADD THIS LINE
      );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示