【问题标题】:Unnecessary warning for invalid tokens when calling file_get_contents调用 file_get_contents 时对无效令牌发出不必要的警告
【发布时间】:2015-01-10 14:17:20
【问题描述】:

以下代码尝试根据访问令牌为 facebook url 运行 file_get_contents

    $access_token=$ret["oauth_token"];
    $fbid=$ret["oauth_uid"];
    $url ="https://api.facebook.com/method/friends.getAppUsers?format=json&access_token=$access_token";
                try {
                    $content = file_get_contents($url);
                    if ($content === false) {
                        $common_friends = array("error_code" => "something");
                    } else {
                        $common_friends = json_decode($content,true);
                    }
                } catch (Exception $ex) {
                    //Who cares
                }

所有 Facebook 设置都是正确的,唯一的潜在问题是访问令牌不再有效。在这种情况下,我会收到警告

file_get_contents(): php_network_getaddresses: getaddrinfo 失败: 名称或服务未知

如何升级我的代码,所以如果 facebook 用户访问令牌过期/无效,file_get_contents 不会发出警告,而是正常失败?

编辑:

$content = @file_get_contents($url);

仍然显示相同的警告,但是,我想摆脱它。 NetBeans 还警告我错误控制运算符被滥用。我修改了我的代码如下:

                try {
                    User::checkFacebookToken();
                    //file_get_contents sends warning message when token does not exist
                    //the problem is already handled, therefore these warnings are not needed
                    //this is why we set scream_enabled to false temporarily
                    ini_set('scream.enabled', false);
                    $content = file_get_contents($url);
                    ini_set('scream.enabled', true);
                    if ($content === false) {
                        $common_friends = array("error_code" => "something");
                    } else {
                        $common_friends = json_decode($content,true);
                    }
                } catch (Exception $ex) {
                    //Who cares
                }

希望scream.enabled只是暂时改成false。

【问题讨论】:

    标签: php facebook file-get-contents


    【解决方案1】:

    您可以或者使用cURL,它具有更好的服务器响应处理能力,并且可以优雅地显示错误。

    $access_token = 'your_access_token';
    $curl = curl_init();
    
    curl_setopt($curl, CURLOPT_URL, "https://api.facebook.com/method/friends.getAppUsers?format=json&access_token=$access_token"); 
    curl_setopt($curl, CURLOPT_VERBOSE, 0); 
    curl_setopt($curl, CURLOPT_HEADER, 0); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    
    $content = curl_exec($curl);
    $response = curl_getinfo($curl);
    echo '<pre>';
    print_r($response);
    if(!curl_errno($curl)){ 
        // Server request success, but check the API returned error code
        $content = json_decode($content, true);
        print_r($content);
        echo $content['error_code'];
        echo $content['error_msg'];
    }else {
        // Server request failure
        echo 'Curl error: ' . curl_error($curl);
    }
    
    curl_close($curl);
    

    PhpFiddle

    【讨论】:

      【解决方案2】:

      解决方案 1:确保不会在主输出上打印警告
      只需将 '@' 添加到 'file_get_contents()' 即可抑制警告,因为您通过以下方式正确捕获了错误:

      error_reporting(E_ERROR | E_PARSE);
      if(@file_get_contents() === FALSE) {
          // handle this error/warning
      }
      

      解决方案 2:在执行请求之前验证您是否拥有有效的 Facebook 令牌,使用 Facebook API

      $ch = curl_init();
      // you should provide your token
      curl_setopt($ch, CURLOPT_URL, "http://graph.facebook.com/me?access_token=52524587821444123");
      curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($c, CURLOPT_HEADER, false);
      $output = json_decode("[" . curl_exec($c) . "]");
      
       if(is_array($output) && isset($output[0]->error)) {
          print "Error Message : " . $output[0]->error->message . "<br />";
          print "Error Type    : " . $output[0]->error->type    . "<br />";
          print "Error Code    : " . $output[0]->error->code    . "<br />";
       }else {
          // do your job with file_get_contents();
       }
      curl_close($c);
      

      【讨论】:

      • 您的想法不适用于 file_get_contents,因为当 facebook 用户访问令牌不再有效时,我仍然收到警告。我已经更改了我的代码,因此编辑了我的问题。如果您可以使用信息编辑您的答案。
      • 有没有办法抑制 file_get_contents 的警告?将 @ 放在那里不会改变行为。
      • @LajosArpad 尝试我的解决方案 2 来测试 facebook 令牌是否有效,在这种情况下,您可以使用 file_get_contents() 而不会发出任何警告
      猜你喜欢
      • 1970-01-01
      • 2012-08-30
      • 2012-01-09
      • 2019-07-16
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      相关资源
      最近更新 更多