laravel 响应内容必须是实现 __toString() 的字符串或对象是什么,给定的“对象”。意思是为什么我只能在遥控器上得到它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18182073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:15:47  来源:igfitidea点击:

What does The Response content must be a string or object implementing __toString(), "object" given. mean and why do I only get it on the remote?

laravel

提问by Himmators

I'm running an app on PagodaBox. Locally it works fine, but when I push it to pagoda, I get the following error:

我正在 PagodaBox 上运行一个应用程序。在本地它工作正常,但是当我将其推送到宝塔时,出现以下错误:

The Response content must be a string or object implementing __toString(), "object" given.

响应内容必须是字符串或实现 __toString() 的对象,给定的“对象”。

Here is the route:

这是路线:

Route::get('tweets/{q}', function($q)
{
    return Twitter::getSearch(array('q' => $q, 'count' => 5, 'lang' => 'sv', 'result_type' => 'recent'));
});

Other routes work fine. What does this error mean and what could cause this error?

其他路线工作正常。这个错误是什么意思,什么可能导致这个错误?

回答by DinkoM

Do this to see what is returned.

执行此操作以查看返回的内容。

dd(Twitter::getSearch(array('q' => $q, 'count' => 5, 'lang' => 'sv', 'result_type' => 'recent')));

You have to return View class which has __toString() method or if it's ajax request then something like Response::json($data);

您必须返回具有 __toString() 方法的 View 类,或者如果它是 ajax 请求,则返回类似 Response::json($data); 的内容。

回答by Juan Manuel Garcia Carmona

create the following function

创建以下函数

function utf8_encode_deep(&$input) {
    if (is_string($input)) {
        $input = utf8_encode($input);
    } else if (is_array($input)) {
        foreach ($input as &$value) {
            self::utf8_encode_deep($value);
        }

        unset($value);
    } else if (is_object($input)) {
        $vars = array_keys(get_object_vars($input));

        foreach ($vars as $var) {
            utf8_encode_deep($input->$var);
        }
    }

try to do the following

尝试执行以下操作

$response = Twitter::getSearch(array('q' => $q, 'count' => 5, 'lang' => 'sv', 'result_type' => 'recent'));

utf8_encode_deep($response);

return response;