我想获取此Web Page的内容
使用 php 函数(例如 file_put_contents
、curl_init
等),但我得到 ��������
作为响应。
为什么会发生这种情况?
我解决了我的问题。获取内容网页后,我使用 mb_convert_encoding($body_webpage,"UTF-8","GBK") 现在可以将其保存在 mysql 中,并带有中文字符。
最佳答案
为了使中文页面正确显示,您需要执行一些操作。
告诉 PHP 我们正在使用 UTF-8 字符串直到脚本结束
mb_internal_encoding('UTF-8');
告诉 PHP 我们将向浏览器输出 UTF-8
mb_http_output('UTF-8');
告诉 Bowser 我们将使用 UTF-8 字符集
header('Content-Type: text/xml; charset=UTF-8');
<小时/>
<小时/>
我已使用以下代码成功加载了具有正确字符编码的页面:
<?php
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.chinanews.com/rss/scroll-news.xml");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_ENCODING, "");
$pagebody=curl_exec ($ch);
curl_close ($ch);
header('Content-Type: text/xml; charset=UTF-8');
echo $pagebody;
?>
<小时/>
了解有关 utf-8 字符编码的更多信息
关于php - 读取特定地址的内容网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30247460/