流明不会在所有国家/地区返回数据

问题描述 投票:0回答:1

正在尝试获取lumen api中所有国家/地区的列表,但是当我返回这样的所有数据时

 echo json_encode([
            'success' => true,
            'data' => $results
        ]);
or

 return json_encode([
            'success' => true,
            'data' => $results
        ]);

or

 return response([
            'success' => true,
            'data' => $results
        ]);

i just get nothing no data no error

我仅在我限制(15)时才获取数据

这是我获取国家列表的功能

        $output = [];
        $database = \DB::table('country_t')
            ->select(['nameX3'])->get();

        foreach ($database as $item) {
            $name = $item->nameX3;
            $toLowerCase = strtolower(trim($name));
            $urlName = str_replace(' ', '-', $toLowerCase);

            $output[] = [
                'name' => $item->nameX3,
                'url' => $urlName . '-simcards',
            ];
        }

        return response([
            'success' => true,
            'data' => $output
        ]);

所以此函数不返回任何东西,除非当我dd($output); or limit(15)

请帮助

提前感谢。

php laravel lumen
1个回答
0
投票

感谢@porloscerrosΨ评论,我至少能够得到一个错误。使用return response()->json(['success' => true, 'data' => $output]);后,出现错误,提示Malformed UTF-8 characters, possibly incorrectly encoded

问题是由json_encode编码的数据甚至包含非utf8字符,例如俄语单词等。>

为了解决这个问题,我创建了一个函数,该函数将使用mb_convert_encoding

对该数据进行编码,因此我还必须使其能够处理多维数组

    function toUTF8( $mixed ) {
        if (is_array($mixed)) {
            foreach ($mixed as $key => $value) {
                $mixed[$key] = toUTF8($value);
            }
        } elseif (is_string($mixed)) {
            return mb_convert_encoding($mixed, "UTF-8", "UTF-8");
        }
        return $mixed;
    }

  Then calling the function 

  $this->toUTF8($output);

总共,如果您的数据包含非utf8字符,则必须对数据进行编码。

© www.soinside.com 2019 - 2024. All rights reserved.