在我的网站上,用户可以通过按钮分享他们的reflinks。问题是在Facebook上发布很慢。我的代码如下:
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);
}
}
我调试了代码,'$ facebook-> api()'大约需要7秒。 Facebook api是如此之慢?或者有什么问题?
编辑: 我正在调试,我注意到BaseFacebook中的函数makeRequest很慢:
var_dump(time()); // i add this
$result = curl_exec($ch);
var_dump(time().'</br>'); // i add this
输出:
int(1387198153) int(1387198158) //5 seconds
int(1387198158) int(1387198163) //5 seconds
int(1387198164) int(1387198171) //7 seconds
问题在于curl_exec()
,但出了什么问题?
答案 0 :(得分:2)
Facebook使用的协议IPv6存在问题。解决方法是将curl设置为仅使用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
);