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

Php Facebook API发布照片/状态更新速度非常慢,php,facebook,Php,Facebook,我将用户存储在数据库中。我用一个循环调用这些函数,但每次发布大约需要5到15秒。使它变慢的不是数据库代码,而是发布功能。这不是全部的代码,但在我看来是最重要的 现在看一下代码,我想知道是否需要在循环中调用函数GetContentsUsingCurl()。有人帮我写了这段代码,但速度很慢 所以我正在努力收紧它 在循环中调用这些函数的示例代码: /// loop code { PostPhoto($fbId, $access_token); PostText($fbId, $access_token

我将用户存储在数据库中。我用一个循环调用这些函数,但每次发布大约需要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;
    }
函数后照($fbId,$access\u token)
{
全球$picu URL;
全球$picu标题;
$url='1https://graph.facebook.com/“.$fbId./photos”;
$attachment=array(
“访问令牌”=>$access\u令牌,
“url”=>$PIC\u url
);
$result=GetContentsUsingCurl($url,$attachment);
$result=json_decode($result,TRUE);
如果(isset($result['error']))
{
回显“错误消息:”.$result['Error']['Message'].“
”;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
回显“错误类型:”.$result['Error']['Type'].“
”;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
回显“错误代码:”.$result['Error']['Code'].“
”;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
} 其他的 { 回声“;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
回显“照片发布成功!
”;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
} } 函数PostText($fbId,$access\u令牌) { 全球$TWEET_URL; 全球$TEXT_消息; 全球$AD; $url='1https://graph.facebook.com/“.$fbId”。/feed”; $tweet=GetContentsUsingCurl($tweet\uURL,“”); $tweet=“\”.trim($tweet)。“\”\n\n”; $attachment=array( “访问令牌”=>$access\u令牌, 'message'=>$tweet.$TEXT\u message.$AD ); $result=GetContentsUsingCurl($url,$attachment); $result=json_decode($result,TRUE); 如果(isset($result['error'])) { 回显“错误消息:”.$result['Error']['Message'].“
”;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
回显“错误类型:”.$result['Error']['Type'].“
”;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
回显“错误代码:”.$result['Error']['Code'].“
”;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
} 其他的 { 回声“;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
echo“Feed发布成功!
”;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
} } 函数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_头,0); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $result=curl\u exec($ch); 卷曲关闭($ch); 返回$result; }
由于您为整个列表发送了相同的tweet,您肯定可以从中获得一些改进:

然后将PostText函数更改为:

函数PostText($fbId,$access\u token,$message)
{
$url='1https://graph.facebook.com/“.$fbId”。/feed”;
$attachment=array(
“访问令牌”=>$access\u令牌,
'message'=>$message
);
$result=GetContentsUsingCurl($url,$attachment);
$result=json_decode($result,TRUE);
如果(isset($result['error']))
{
回显“错误消息:”.$result['Error']['Message'].“
”;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
回显“错误类型:”.$result['Error']['Type'].“
”;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
回显“错误代码:”.$result['Error']['Code'].“
”;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
} 其他的 { 回声“;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
echo“Feed发布成功!
”;
$tweet = GetContentsUsingCurl($TWEET_URL, "");
$message = $tweet.$TEXT_MESSAGE.$AD;
/// loop code
{
  PostPhoto($fbId, $access_token);
  PostText($fbId, $access_token, $message);
}
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/>";
    }
}
} }
从url模式判断,我认为您没有什么可以做的。我会尝试通过设置一批用户ID和tweet并发布一篇大文章(即一次发送100条tweet)来优化这样的任务。不过,Facebook必须支持这一点。我注意到一件事。有没有可能他们每次都在做同样的事情,给网站打电话,为每一篇文章一次又一次地修改它们。我们是否有可能将其作为一个全局var并调用一次?下面是几行:$tweet=GetContentsUsingCurl($tweet\uURL,“”)$tweet=“\”.trim($tweet)。“\”\n\n”;你会一直在整个用户列表中发布相同的推文吗?如果是这样的话,一定要把这些线从回路中拉出来。您可以使用全局变量或将
PostText
更改为
函数PostText($fbId,$access\u token,$tweet)
是,相同的tweet。好吧,我想是的。谢谢