沃梦达 / 编程问答 / php问题 / 正文

PHP Curl UTF-8 字符集

PHP Curl UTF-8 Charset(PHP Curl UTF-8 字符集)

本文介绍了PHP Curl UTF-8 字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 php 脚本,它调用另一个网页并写入页面的所有 html,一切正常,但是存在字符集问题.我的 php 文件编码是 utf-8 并且所有其他 php 文件都可以正常工作(这意味着服务器没有问题).该代码中缺少什么,所有西班牙语字母看起来都很奇怪.附注.当我将这些奇怪的字符原始版本写入php时,它们看起来都很准确.

I have an php script which calls another web page and writes all the html of the page and everything goes ok however there is a charset problem. My php file encoding is utf-8 and all other php files work ok (that means there is no problem with server). What is the missing thing in that code and all spanish letters look weird. PS. When I wrote these weird characters original versions into php, they all look accurate.

header("Content-Type: text/html; charset=utf-8");
function file_get_contents_curl($url)
{
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    $data=curl_exec($ch);
    curl_close($ch);
    return $data;
}
$html=file_get_contents_curl($_GET["u"]);
$doc=new DOMDocument();
@$doc->loadHTML($html);

推荐答案

简单:当您使用 curl 时,它会将字符串编码为 utf-8 您只需要解码它们..

Simple: When you use curl it encodes the string to utf-8 you just need to decode them..

Description

string utf8_decode ( string $data )

此函数将假定为 UTF-8 编码的数据解码为 ISO-8859-1.

This function decodes data , assumed to be UTF-8 encoded, to ISO-8859-1.

这篇关于PHP Curl UTF-8 字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PHP Curl UTF-8 字符集