curl_init回来的编码怎么判断?个别文字乱码求解!
代码如下:
<?php$url = "http://zhidao.baidu.com/link?url=pTwcJotQ02pjg-mjCnc-fkw8ONOY9x8q0ESrCFhdVJy47agZnDnCb-BCAtngRGDt9yi0TvleSS_w0aPj8Vsk0atVkVhNYdZADN0kv0BzNau";echo fopen_url($url);function fopen_url($url) { if (function_exists('curl_init')) { $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, $url); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1); curl_setopt($curl_handle, CURLOPT_FAILONERROR,1); curl_setopt($curl_handle, CURLOPT_TIMEOUT,2); $file_content = curl_exec($curl_handle); $encode = mb_detect_encoding($file_content, array("ASCII","UTF-8","GB2312","GBK","BIG5")); if($encode != "UTF-8") { $file_content = mb_convert_encoding($file_content, "UTF-8", $encode); //$file_content = iconv($encode,'utf-8//IGNORE',$file_content); } curl_close($curl_handle); } else { $file_content = ''; } return $file_content; }?>
个别字符竟然会乱码!奇怪,请看图
这什么原因造成的?
上面的代码貌似有误啊,原页面明明是GB2312 缺判断出是CP936,无语啊
请帮忙看看上面代码是不是需要完善一下
非常感谢!
回复讨论(解决方案)
另外还有个别页面竟然curl_init回来是空白的数据,要多刷新几次才显示,参数值设置有问题?
返回的数据中有:
根据他就可知道页面编码
没有时才需要编程判断
mb_detect_encoding 判断常有失误,所以又增加了 mb_check_encoding 函数
数据片段
没理由出现非法字符
CP936 是 GBK 的国际称谓
第一个问题,不是乱码,那是图片,curl抓取百度页面,会特意把某些文字转换成图片,防抓取。你查看网页元素,你就会发现,那些乱码其实是百度的图片地址。
第二个问题,你把超时时间设置大点,就好了,可能是你网络问题。
第一个问题,不是乱码,那是图片,curl抓取百度页面,会特意把某些文字转换成图片,防抓取。你查看网页元素,你就会发现,那些乱码其实是百度的图片地址。
第二个问题,你把超时时间设置大点,就好了,可能是你网络问题。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...
