我将用户存储在数据库中。我在循环中用这些函数调用它们,但每个帖子大约需要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;
}
由于您正在为整个列表发送相同的推文,因此您肯定可以从中获得一些改进:
$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/>";
}
}