【发布时间】:2012-09-10 11:32:29
【问题描述】:
我正在尝试执行以下操作。
当我尝试使用 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。
有什么想法吗?
【问题讨论】:
-
你尝试块很可能不会抛出那个又长又丑的异常。由于您只捕获该特定异常,因此任何其他异常都会继续进行。
-
如果你改为
catch (Exception $e)是否有效? -
现在似乎正在使用 Exception $e,我之前尝试过,但运气不佳。也许它被缓存或什么的。无论如何,谢谢,您可以将其发布为答案吗?
标签: php oauth error-handling oauth-2.0