我正在将Facebook登录选项集成到我正在工作的站点上,在用PHP调用Facebook的API时遇到了一些问题。我得到的实际错误如下:
未能连接到graph.facebook.com端口443:连接超时
我已经验证了curl调用上的超时值是正确的,我尝试通过SSH直接从服务器的命令提示符检查连接(pings正确,端口似乎是打开的,进程正在监听它,等等)。但是,使用其他有类似问题的人在网上找到的一个简单的卷曲片段,我成功地测试了它,似乎这些问题是断断续续的/不一致的:有时它运行得完美无缺,速度很快,有时加载了几秒钟,上面的错误失败了。
我非常怀疑这个错误是否站在Facebook的一边,但我不知道我做错了什么。我以前在PHP中使用过Facebook,这是我第一次看到这个错误。
以前有没有其他人面对过这个问题,并加以解决?
请注意:这是我第一次在基于Symfony的项目中与Facebook合作--不要认为这与此相关,只是为了以防万一。
相关片段:
$fb = new \Facebook\Facebook(['app_id' => '[withheld]',
'app_secret' => '[withheld]',
'default_graph_version' => 'v2.10',]);
$fb->setDefaultAccessToken($accessToken); # Access token obtained from Facebook Login in JS, passed in post data.
try {
$basic_info_response = $fb->get('/me?fields=id,first_name,last_name,email,website');
if ($with_picture)
$profile_picture_response = $fb->get('/me/picture?width=720&height=720');
if ($with_friends)
$friends_response = $fb->get('/me/friends');
}
catch(\Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}
catch(\Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
在这段代码中,它在$fb->get()调用中失败--并不总是相同的调用,有时在第一个调用,有时在图片调用,有时在好友调用。
更新
这个错误仍然在发生,在我90%的电话上。我试图通过SSH (curl -v graph.facebook.com
)在服务器上直接执行curl调用,并得到了以下两个结果:
* Rebuilt URL to: graph.facebook.com/
* Trying 31.13.91.2...
* TCP_NODELAY set
* Trying 2a03:2880:f01b:1:face:b00c:0:1...
* TCP_NODELAY set
* Immediate connect fail for 2a03:2880:f01b:1:face:b00c:0:1: Network is unreachable
* Connected to graph.facebook.com (31.13.91.2) port 80 (#0)
> GET / HTTP/1.1
> Host: graph.facebook.com
> User-Agent: curl/7.50.2
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "Unsupported get request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api"
< Access-Control-Allow-Origin: *
< Pragma: no-cache
< Cache-Control: no-store
< x-fb-rev: 3274377
< Content-Type: application/json; charset=UTF-8
< x-fb-trace-id: A2OYZzFP3v8
< facebook-api-version: v2.4
< Expires: Sat, 01 Jan 2000 00:00:00 GMT
< Vary: Accept-Encoding
< X-FB-Debug: z9FEtq3Rlh8LyFn6pOIBZ5ZMCX+TY1jUD7iZ7ZRZ8/YGAsi035TbCP3qdBzqxDryvjJhKoKxnbAdvcxY/7r3Vg==
< Date: Mon, 04 Sep 2017 19:51:40 GMT
< Transfer-Encoding: chunked
< Connection: keep-alive
<
* Curl_http_done: called premature == 0
* Connection #0 to host graph.facebook.com left intact
和
* Rebuilt URL to: graph.facebook.com/
* Trying 31.13.91.2...
* TCP_NODELAY set
* Connected to graph.facebook.com (31.13.91.2) port 80 (#0)
> GET / HTTP/1.1
> Host: graph.facebook.com
> User-Agent: curl/7.50.2
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "Unsupported get request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api"
< Access-Control-Allow-Origin: *
< Pragma: no-cache
< Cache-Control: no-store
< x-fb-rev: 3274377
< Content-Type: application/json; charset=UTF-8
< x-fb-trace-id: BrIDaH8D8D5
< facebook-api-version: v2.4
< Expires: Sat, 01 Jan 2000 00:00:00 GMT
< Vary: Accept-Encoding
< X-FB-Debug: tYvsf7Nn2PVnHFkV40UUddjQGKzPl8XKfdNeiqu1CXZck5WuUUlcG9hoCAZQrOX93uS19m2JpAEu9DJ/YhSIeg==
< Date: Mon, 04 Sep 2017 19:54:54 GMT
< Transfer-Encoding: chunked
< Connection: keep-alive
<
* Curl_http_done: called premature == 0
* Connection #0 to host graph.facebook.com left intact
有没有人对这个问题有更多的信息或可能的解释?
发布于 2017-09-08 02:10:03
事实证明,除了定期超时之外,Facebook的SDK中还设置了一个硬编码的CURLOPT_CONNECTTIMEOUT
--这似乎就是造成这个问题的原因。每当我的服务器和Facebook之间的连接太慢(10s+)时,就会超时,因为10是SDK中的默认值。
我将这个值更改为cURL的默认值,这个选项是300,然后它又开始工作了。此值在Facebook的SDK中FacebookCurlHttpClient
类中的openConnection
方法中设置。
现在,对于导致连接在某些时候如此缓慢的原因,这仍然是未知的,但至少当这种情况发生时,它不再崩溃。
您可以重新定义SDK的cURL包装器的方法,如下所示:https://www.sammyk.me/how-to-inject-your-own-http-client-in-the-facebook-php-sdk-v5#customizing-the-curl-predefined-constants
例如,下面是我重新定义的方法,允许在超时之前使用更长的时间:
<?php
namespace AppBundle\Lib\Facebook;
class CustomCurlOptsHttpClient extends \Facebook\HttpClients\FacebookCurlHttpClient
{
public function send($url, $method, $body, array $headers, $timeOut)
{
$timeOut *= 5;
return parent::send($url, $method, $body, $headers, $timeOut);
}
public function openConnection($url, $method, $body, array $headers, $timeOut)
{
$timeOut *= 5;
parent::openConnection($url, $method, $body, $headers, $timeOut);
$options = [
CURLOPT_CONNECTTIMEOUT => 300,
];
$this->facebookCurl->setoptArray($options);
}
}
如果有多个请求要一个接一个地发出,还可以在Facebook中使用批处理请求。这将有助于加快进程,并防止你打到超时。
希望这能帮助其他面临同样问题的人!
发布于 2017-09-02 05:02:43
我不能评论,但你能提供一些代码吗?将乐意帮助和调试此问题。
此外,Facebook确实会在某些东西阻塞您的请求时返回此错误。例如,file_get_contents在许多共享主机服务器上被阻塞。
您还应该处理错误。
发布于 2017-09-07 22:22:00
下面是我正在使用的一个示例片段,它可以很好地为我工作。
<?php
include 'configs.php';
include_once "Facebook/autoload.php";
$swipe=1;
$goto=null;
try {
if (!isset($_SESSION['FACEBOOK_SESSION_TOKEN'])) {
$fb = new Facebook\Facebook([
'app_id' => APP_ID,
'app_secret' => APP_SECRET,
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ["user_about_me","publish_actions" , "user_photos"];
$loginUrl = $helper->getLoginUrl( CALLBACK_URL , $permissions);
$swipe=0;
}
}
catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}?>
Config.php
<?php
define("CALLBACK_URL", "http://{domain}/facebookredirect.php");
define("RESULT_PAGE", "http://{domain}//profile.php");
define("LOGIN_URI" , "http://{domain}//index.html");
define("APP_ID" , "########");
define("APP_SECRET" ,"#####");
?>
另外,Graph没有100%的正常运行时间。检查您的服务器是否工作正常。
https://stackoverflow.com/questions/46009874
复制相似问题