2012-09-10 111 views
0

我正在嘗試以下操作。PHP catch無法打開流:HTTP請求失敗! HTTP/1.1 401未授權

當我嘗試使用oAuth訪問服務時,如果出現錯誤,因爲我使用了錯誤的密鑰,我不想顯示錯誤消息,而是將用戶發送到身份驗證頁面。

但是使用try catch fn不起作用。

這是我的代碼:

$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。

任何想法?

+0

您嘗試塊是最有可能不扔那大長醜陋的例外。既然你只捕捉那個特定的異常,任何其他異常就會繼續。 –

+1

如果你捕獲(Exception $ e)'而不是它工作? –

+0

可能會幫助你http://stackoverflow.com/questions/9574130/file-get-contents-failed-to-open-stream-http-request-failed-http-1-1-404-not –

回答

相關問題