首页 > 解决方案 > Laravel 生产错误使用 Guzzle HTTP 获取 URL 状态码

问题描述

我遇到了一个问题,我的laravel.log文件中出现以下错误。

[2020-12-20 13:43:43] production.ERROR: URI must be a string or UriInterface

但是我的代码在开发中工作,这非常奇怪。我试图弄清楚为什么这不起作用,我真的很茫然,没有多少谷歌搜索或搜索在这里回答了我关于为什么在开发与生产中会发生此错误的问题。

这是我的功能:

/**
 * Returns the user avatar or the placeholder avatar
 * depending on the Discord Status Code
 *
 * @param \App\Models\Auth\User $user
 * @return \Illuminate\Http\RedirectResponse|mixed|string
 */
public static function getUserAvatar(\App\Models\Auth\User $user) {
    $client = new Client();

    try {
        $response = $client->head($user->avatar);
    } catch(BadResponseException $e) {
        return "https://dummyimage.com/128x128/888ea8/ebedf2.png?text=No+Image+Found";
    }

    if($response->getStatusCode() == 200) {
        return $user->avatar;
    } else {
        return "https://dummyimage.com/128x128/888ea8/ebedf2.png?text=No+Image+Found";
    }
}

主要功能是……每当用户登录时,他们的头像都会从不和谐中更新(使用社交名流作为登录提供者)。但是,如果用户在 discord 中更改了他们的头像,但有一段时间没有登录我们的网站,则该头像链接将失效,因为 discord 不再希望为其提供服务。所以头像链接返回404码。我基本上只是想处理这个 404 代码,而不是在网站上显示损坏的图像。相反,它只是一个占位符图像。

编辑:当我死并转储时,$user->avatar我得到以下结果:

Die and Dump Image Avatar

我也尝试了以下方法,但无济于事:

$response = $client->request('GET', (string) $user->avatar, ['allow_redirects' => false, 'verify' => false]);

谢谢。

标签: laraveldiscordguzzle

解决方案


推荐阅读