【问题标题】:Failed to connect to graph.facebook.com port 443: Network unreachable无法连接到 graph.facebook.com 端口 443:网络无法访问
【发布时间】:2014-09-27 16:29:44
【问题描述】:
<?php
session_start();

//included file and use

$app_id = 'xxx';
$app_secret = 'xxx';
FacebookSession::setDefaultApplication($app_id, $app_secret);
$helper = new FacebookRedirectLoginHelper("`http://example/facebook4.0`/", $app_id, $app_secret);
try 
{
    $session = $helper->getSessionFromRedirect();
}
catch(FacebookRequestException $ex) { } 
catch(Exception $ex) { }

$loggedIn = false;

if (isset($session))
{
    if ($session) 
        {
            $loggedIn = true;
            try {                     //logged here and get data
                $user_profile = (new FacebookRequest(
                $session, 'GET', '/me'
                ))->execute()->getGraphObject(GraphUser::className());

               print_r($user_profile); //print data

            } 
            catch(FacebookRequestException $e)  {
                echo "Exception occured, code: " . $e->getCode();
                echo " with message: " . $e->getMessage();
            }   
        }
}

if(!$loggedIn)  //if user is not online // get link and add scope
{
    $loginUrl = $helper->getLoginUrl(array('public_profile','email'));
    echo "<a href='$loginUrl'>Login With Facebook</a>";
}
else
{
    print_r($user_profile); //logout link is generated here 
    echo '<br><br><a href="index.php">Logout</a>'; //print to sceen
}

?>

我已将域名设置为 localhost,将站点 URL 设置为 http://localhost/。 我也试过http://localhost:80http://mydomainname:80(更换主机)

这是我收到的错误回复:

Facebook\FacebookSDKException Object
(
    [message:protected] => Failed to connect to graph.facebook.com port 443: Network unreachable
    [string:Exception:private] => 
    [code:protected] => 7
    [file:protected] => C:\xampp\htdocs\myapp\Facebook\HttpClients\FacebookCurlHttpClient.php
    [line:protected] => 142
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => C:\xampp\htdocs\myapp\Facebook\FacebookRequest.php
                    [line] => 248
                    [function] => send
                    [class] => Facebook\HttpClients\FacebookCurlHttpClient
                    [type] => ->
                    [args] => Array
                        (
                            [0] => https://graph.facebook.com/v2.0/oauth/access_token?client_id='my key'&redirect_uri=http%3A%2F%2Flocalhost%3A80%2Fmyapp%2F&client_secret='mykey_secert'&code=AQCYmzsFNUIQG7gUAZ3y-YJHLeIGcF-xyqHotx31MCJGlm16fV9VbVSzlGlx5280-u0Ho3jFjg_REevN5J0LEIPHerY1QaaBYjpkkoIMf6PCwHGj2OIrQDvfyGcUJRK4cJP0YQ8H8HdYw86xEhlcdJHvnObkCU6tSBcVbDWM8uoXJlRqNl6o-IdxoSfbk6IjuCreyagMXvam4vgV0HKxn0nkWaV26k1P6kQP_L1LtXXx2UyUQ1i0jJGL9JiGr1gsUbf5drY_URIrEWzawumpnSWkuxln8hiOtAr_xwM_4cBZwxf3_pWq8YnUotpmzzM5sPhW_ERMYWAdovjZPHu7Xdgs&access_token=329734847193179%7Cd9e5cdb2d0c3cbe1e127827762e94284&appsecret_proof='key_proof'
                            [1] => GET
                            [2] => Array
                                (
                                )    
                        )    
                )

            [1] => Array
                (
                    [file] => C:\xampp\htdocs\myapp\Facebook\FacebookRedirectLoginHelper.php
                    [line] => 146
                    [function] => execute
                    [class] => Facebook\FacebookRequest
                    [type] => ->
                    [args] => Array
                        (
                        )    
                )

            [2] => Array
                (
                    [file] => C:\xampp\htdocs\myapp\index.php
                    [line] => 52
                    [function] => getSessionFromRedirect
                    [class] => Facebook\FacebookRedirectLoginHelper
                    [type] => ->
                    [args] => Array
                        (
                        )    
                )    
        )    
    [previous:Exception:private] => 
)

【问题讨论】:

  • 1.你能ping通这个地址吗? 2. 您希望 facebook 服务器如何访问您的本地主机? facebook 可能不会重定向到它无法到达的地址。
  • 但是在 7 月 31 日之前,Working 和上述代码在我的本地主机上运行的代码相同。突然他们从 8 月 1 日停止了你还能帮我吗?
  • Facebook 永远不会访问您的服务器,因此访问 localhost 不是问题。确保您的服务器设置为允许传出连接。另外,看看这是否有效:$data = file_get_contents('https://graph.facebook.com/4'); print_r( $data );
  • 你能发布结果吗:$ch = curl_init("https://google.com"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $data = curl_exec($ch); print($data);
  • 它打印 bool(false)

标签: php facebook facebook-graph-api facebook-php-sdk


【解决方案1】:

我知道这是一个老问题,OP 可能已经找到了解决方案。在使用Laravel Socialite 插件时,我也遇到了同样的问题。

为了后代,我想在这里发布解决方案。此question 中解释了该问题:域 graph.facebook.com 解析为 IPV6 地址,某些网络可能未配置为路由。您可以请求 cURL 将 IP 地址解析为其 IPV4 值。将选项CURLOPT_IPRESOLVE 设置为CURL_IPRESOLVE_V4

对于社交名流插件,我们必须修改 FacebookProvider.php 以包含附加选项。

编辑

更具体地说,Socialite 使用 GuzzleHttp,我们需要将此作为参数传递给 GuzzleHttp Client 方法:

'curl' => [ CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4]

【讨论】:

  • 我们需要在哪个文件中包含这段代码?谢谢
  • 谢谢!拯救了我的一天
猜你喜欢
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 1970-01-01
  • 2011-12-03
  • 2016-09-05
  • 1970-01-01
相关资源
最近更新 更多