Facebook API发布照片/状态更新非常慢

时间:2013-12-10 02:13:30

标签: php facebook

我将用户存储在数据库中。我在循环中用这些函数调用它们,但每个帖子需要5到15秒。它不是数据库代码,而只是使它变慢的发布函数。这不是所有代码,但在我看来是最重要的。

现在我查看代码,我想知道是否需要在循环中调用函数GetContentsUsingCurl()。有人帮我写了这段代码,但速度很慢 所以我试图收紧它。

示例代码在循环中调用这些函数:

/// loop code
{
PostPhoto($fbId, $access_token);
PostText($fbId, $access_token);
}

我的问题:我能以某种方式加快速度吗?

function PostPhoto($fbId, $access_token)
    {
        global $PIC_URL;
        global $PIC_CAPTION;

        $url = 'https://graph.facebook.com/' . $fbId . '/photos';
        $attachment =  array(
                'access_token'  => $access_token,
                'url'           => $PIC_URL
        );

        $result = GetContentsUsingCurl($url, $attachment);
        $result = json_decode($result, TRUE);
        if( isset($result['error']) ) 
        {
            echo "Error Message: ".$result['error']['message']."<br/>";
            echo "Error Type: ".$result['error']['type']."<br/>";
            echo "Error Code: ".$result['error']['code']."<br/>";
        }
        else
        {
            echo "<pre>";
            echo "Photo posted successfully!<br/>";
        }
    }

    function PostText($fbId, $access_token)
    {
        global $TWEET_URL;
        global $TEXT_MESSAGE;
        global $AD;

        $url = 'https://graph.facebook.com/' . $fbId . '/feed';
        $tweet = GetContentsUsingCurl($TWEET_URL, "");
        $tweet = "\"".trim($tweet)."\"\n\n"; 
        $attachment =  array(
                'access_token'  => $access_token,
                'message'  => $tweet.$TEXT_MESSAGE.$AD
        );

        $result = GetContentsUsingCurl($url, $attachment);
        $result = json_decode($result, TRUE);
        if( isset($result['error']) ) 
        {
            echo "Error Message: ".$result['error']['message']."<br/>";
            echo "Error Type: ".$result['error']['type']."<br/>";
            echo "Error Code: ".$result['error']['code']."<br/>";
        }
        else
        {
            echo "<pre>";
            echo "Feed posted successfully!<br/>";
        }
    }


    function GetContentsUsingCurl($url, $attachment)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close ($ch);

        return $result;
    }

1 个答案:

答案 0 :(得分:1)

由于您要为整个列表发送相同的推文,因此您可以通过这样做获得一些改进:

$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}

然后将PostText函数更改为:

function PostText($fbId, $access_token, $message)
{
    $url = 'https://graph.facebook.com/' . $fbId . '/feed';
    $attachment =  array(
            'access_token'  => $access_token,
            'message'  => $message
    );

    $result = GetContentsUsingCurl($url, $attachment);
    $result = json_decode($result, TRUE);
    if( isset($result['error']) ) 
    {
        echo "Error Message: ".$result['error']['message']."<br/>";
        echo "Error Type: ".$result['error']['type']."<br/>";
        echo "Error Code: ".$result['error']['code']."<br/>";
    }
    else
    {
        echo "<pre>";
        echo "Feed posted successfully!<br/>";
    }
}