0

I am trying to do the following.

When I try to access a service using oAuth, if I get an error, because I am using a wrong key, I want to not show the error messages, but instead send the user to the authentication page.

However using a try catch fn does not work.

This is my code:

$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;
    }

I want this to either return the user array or FALSE.

Any ideas?

Lukas Oppermann
  • 2,918
  • 6
  • 47
  • 62
  • You try block is most likely NOT throwing that big long ugly exception. Since you're catching only that SPECIFIC exception, any other exception will just proceed on its way. – Marc B Sep 10 '12 at 03:30
  • 1
    Does it work if you `catch (Exception $e)` instead? – Kenny Linsky Sep 10 '12 at 03:32
  • may this help you http://stackoverflow.com/questions/9574130/file-get-contents-failed-to-open-stream-http-request-failed-http-1-1-404-not – Arpit Srivastava Sep 10 '12 at 03:32
  • It seems to be working with Exception $e now, I tried it before with not luck. Maybe it was cached or something. Thanks anyway, can you post it as an answer? – Lukas Oppermann Sep 10 '12 at 03:55

1 Answers1

5

You can take a look here for catching file_get_content error

How can I handle the warning of file_get_contents() function in PHP?

That offers different solutions like looking at the response header

$content = @file_get_contents("http://www.google.com");
if (strpos($http_response_header[0], "200")) { 
   echo "SUCCESS";
} else { 
   echo "FAILED";
}
Community
  • 1
  • 1
Elyx0
  • 349
  • 2
  • 10