PHP中的Facebook Graph API不一致 - 无法连接到Graph.facebook.com端口443:连接



我正在在正在研究的网站上集成一个Facebook登录选项,并且在PHP中拨打Facebook的API时,我遇到了一些问题。我得到的实际错误如下:

无法连接到graph.facebook.com端口443:连接时间

我已经验证了卷曲调用上的超时值正确,我尝试通过SSH直接从服务器命令提示符检查连接(正确地,端口似乎是打开的,并且进程正在侦听,等等)。但是,使用其他有类似问题的其他人在网上找到的简单卷发片段,我设法轻松测试它,并且似乎这些问题是间歇性/不一致的:有时它可以完美无瑕地工作,有时会加载几秒钟,并且会加载几秒钟,并且上述错误失败。

我高度怀疑该错误在Facebook的一边,但我无法弄清楚我在做什么错。我以前曾在PHP中与Facebook SDK合作,这是我第一次看到此错误。

其他人以前是否遇到过这个问题并解决了问题?

快速注意:这是我第一次在基于Symfony的项目中与Facebook合作 - 在这种情况下,不要以为它是相关的,而是将其扔出去。

相关片段:

$fb = new FacebookFacebook(['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(FacebookExceptionsFacebookResponseException $e) {
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
}
catch(FacebookExceptionsFacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

在此代码中,它在任何$ fb-> get()调用中都失败 - 并非总是相同的,有时在第一个,有时在图片上,有时在一个朋友中。

[update]

我的电话中约有90%仍在发生错误。我尝试通过SSH(curl -v graph.facebook.com)直接在服务器上进行卷曲调用,并得到以下两个结果:

* 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

有人还有其他有关此问题的信息或可能的解释吗?

事实证明,除了常规超时外,还设置了Facebook的SDK中的硬编码CURLOPT_CONNECTTIMEOUT - 这似乎是引起问题的原因。每当我的服务器和Facebook API之间的连接太慢(10s )时,它将超时,因为10S是SDK中的默认值。

我将此值更改为curl的默认值,即300,然后重新开始工作。此值是在FacebookCurlHttpClient类的Facebook的SDK中设置的,openConnection方法。

现在,导致连接在某些时候慢的原因,这仍然未知,但至少在发生这种情况时它不再崩溃了。

您可以重新定义SDK的卷曲包装器的方法,如下所示:https://www.sammyk.me/how-to-ind-to-inject-yound-your-own-http-client-in-http-client-in-the-face-face-face-face-php--SDK-V5#自定义 - 刚刚定义的constants

例如,这是我重新定义的方法,以便在超时之前允许更长的时间段:

<?php
namespace AppBundleLibFacebook;
class CustomCurlOptsHttpClient extends FacebookHttpClientsFacebookCurlHttpClient
{
    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 SDK中使用批处理请求。这将有助于加快流程并阻止您到超时。

希望能帮助其他面临同一问题的人!

我无法发表评论,但是您可以提供一些代码吗?很乐意帮助和调试这个问题。

另外,当某些东西阻止您的请求时,Facebook Graph API确实会返回此错误。例如,File_get_contents在许多共享托管服务器上被阻止。

您还应该处理错误。

这是我正在使用的样本片段,对我来说正常工作。

<?php    
include 'configs.php';
    include_once "Facebook/autoload.php";
    $swipe=1;
    $goto=null;
    try {
        if (!isset($_SESSION['FACEBOOK_SESSION_TOKEN'])) {
            $fb = new FacebookFacebook([
                '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(FacebookExceptionsFacebookResponseException $e) {
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch(FacebookExceptionsFacebookSDKException $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" ,"#####");
   ?>

另外,图API没有100%的正常运行时间。检查服务器的卷发正常工作。

最新更新