0

私は次のことをしようとしています。

oAuthを使用してサービスにアクセスしようとすると、間違ったキーを使用しているためにエラーが発生した場合、エラーメッセージを表示せずに、ユーザーを認証ページに送信します。

ただし、trycatchfnを使用しても機能しません。

これは私のコードです:

$url = 'https://api.soundcloud.com/me.json?'.http_build_query(array(
        'oauth_token' => $token,
    ));
    //
    try{
        $user = json_decode(file_get_contents($url));
        return array(
            'uid' => $user->id,
            'nickname' => $user->username,
            'name' => $user->full_name,
            'location' => $user->country.' ,'.$user->country,
            'description' => $user->description,
            'image' => $user->avatar_url,
            'urls' => array(
                'MySpace' => $user->myspace_name,
                'Website' => $user->website,
            ),
        );
    }
    catch(Services_Soundcloud_Invalid_Http_Response_Code_Exception $e)
    {
        log_message('error', 'EXCEPTION: '.$e);
        return FALSE;
    }

これでユーザー配列またはFALSEを返すようにします。

何か案は?

4

1 に答える 1

5

file_get_contentエラーをキャッチするためにここを見てください

PHPでfile_get_contents()関数の警告を処理するにはどうすればよいですか?

これは、応答ヘッダーを確認するなど、さまざまなソリューションを提供します

$content = @file_get_contents("http://www.google.com");
if (strpos($http_response_header[0], "200")) { 
   echo "SUCCESS";
} else { 
   echo "FAILED";
}
于 2012-09-10T03:32:29.887 に答える