调用file_get_contents时对无效令牌的不必要警告


Unnecessary warning for invalid tokens when calling file_get_contents

以下代码尝试根据访问令牌为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。

您也可以使用 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);

小提琴

解决方案 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);