php - 读取特定地址的内容网页

标签 php mysql utf-8 page-curl file-put-contents

我想获取此Web Page的内容 使用 php 函数(例如 file_put_contentscurl_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 字符编码的更多信息

https://phpbestpractices.org/#utf-8

关于php - 读取特定地址的内容网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30247460/

相关文章:

php - 在哪里可以找到婚介网站的信息/代码? (不一定是约会)

python - Django 每月注释价格

C# Unix 时间戳

python - lxml.html5parser : not working for arabic/persian html5s

perl 输出 - 无法正确打印 utf8 文本文件

java - "illegal character:\65279"在 Maven 编译期间尽管设置为 UTF-8

php - 在 PHP 中提交表单后,表单字段数据存储在哪里?

php - magento - 快速搜索返回所有产品

php - 我的查询中存在语法错误

mysql - Npm Mysql 连接错误