0
我有用戶存儲在數據庫中。我在循環中用這些函數調用它們,但每個帖子需要5到15秒。它不是數據庫代碼,而只是發佈緩慢的發佈函數。這不是所有的代碼,但在我看來最重要的。Facebook API發佈照片/狀態更新很慢
現在我看看代碼,我想知道是否需要在循環中調用函數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;
}
通過url模式判斷,我不認爲你能做的事情很多。我會嘗試通過設置一批用戶ID和推文並製作一個大型POST(即,一次發送100條推文)來優化此類任務。不過,Facebook將不得不支持這一點。 – Brian
我注意到一件事。是否有可能每次都會做同樣的事情,並且每次打電話給網站並重復修改它們。有可能我們可以將其作爲全局變量並調用一次?這裏是行: $ tweet = GetContentsUsingCurl($ TWEET_URL,「」); $ tweet =「\」「。trim($ tweet)。」\「\ n \ n」; –
您是否總是將相同的推文發佈到整個用戶列表?如果是這樣,肯定會將這些線路從循環中取出。您可以使用全局變量或將'PostText'更改爲'函數PostText($ fbId,$ access_token,$ tweet)' – Brian