在数字化转型中,选择合适的跨平台开发框架不仅能提高效率,还有助于确保数据安全与合规性。
655
2022-11-20
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]";}
参考:
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_ $client = new Client(); //GuzzleHttp\Client try { $response = $client->request('GET', ' '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()); } } }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~