laravel的predis報錯亂碼

今天使用redis的時候報錯,但是頁面顯示一塌糊塌,原因是編碼錯誤。

Predis \ Connection \ ConnectionException (10061)
����Ŀ����������ܾ����޷����ӡ�

在這裏插入圖片描述

private function createExceptionMessage($message) {
    $encoding = mb_detect_encoding($message, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5'));
    // 如果字符串的編碼格式不爲UTF_8就轉換編碼格式,檢測代碼,檢測到是"EUC-CN"
    if ($encoding != 'UTF-8') {
        $message = mb_convert_encoding($message, 'UTF-8', $encoding);
    }
    #結束
    $parameters = $this->parameters;

    if ($parameters->scheme === 'unix') {
        return "$message [$parameters->scheme:$parameters->path]";
    }

    if (filter_var($parameters->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
        return "$message [$parameters->scheme://[$parameters->host]:$parameters->port]";
    }

    return "$message [$parameters->scheme://$parameters->host:$parameters->port]";
}

在這裏插入圖片描述

參考:
https://blog.csdn.net/muzitian/article/details/95065002

https://www.jianshu.com/p/67801d24d974

<?php

namespace App\Http\Controllers;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Http\Request;

class HomeController extends Controller
{

    public function test_guzzle_http_get(){

        $client = new Client(); //GuzzleHttp\Client

        try {
            $response = $client->request('GET', 'https://www.baidu.com/',[
                'headers'=> [
                    'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
                    'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                    'Accept-Language' => 'zh-CN,zh;q=0.9,en;q=0.8,sm;q=0.7',
                    'Accept-Encoding' => 'gzip'
                ],
                'decode_content' => true,// 解密gzip
                'connect_timeout' => 10
            ]);

             // print_r($response);

            // 轉換成頁面使用的編碼,默認爲UTF-8,否則亂碼!
            $type = $response->getHeader('content-type');
            $parsed = Psr7\parse_header($type);
            $original_body = (string)$response->getBody();
            $utf8_body = mb_convert_encoding($original_body, 'UTF-8', $parsed[0]['charset'] ?: 'UTF-8');

            print_r($utf8_body);


        } catch (RequestException $e) {
            // 此部分是頁面出錯時輸出,如404
            echo Psr7\str($e->getRequest());
            if ($e->hasResponse()) {
                echo Psr7\str($e->getResponse());
            }
        }

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章