gpt4 book ai didi

PHP catch 无法打开流 : HTTP request failed! HTTP/1.1 401 Unauthorized

转载 作者:行者123 更新时间:2023-12-03 08:04:33 26 4
gpt4 key购买 nike

我正在尝试执行以下操作。

当我尝试使用 oAuth 访问服务时,如果我收到错误,因为我使用了错误的 key ,我不想显示错误消息,而是将用户发送到身份验证页面。

但是使用 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。

有任何想法吗?

最佳答案

您可以查看此处以捕获 file_get_content 错误

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

这提供了不同的解决方案,例如查看响应 header

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

关于PHP catch 无法打开流 : HTTP request failed! HTTP/1.1 401 Unauthorized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12344949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com