search
Homephp教程php手册php中file_get_contents获取网页乱码解决办法

php中file_get_contents获取网页乱码解决办法

Jun 13, 2016 am 09:57 AM
filegetphpGarbled charactersMethodFunctionIhouryesterdaySimpleWeb pageObtainsolvecollection

昨天我在做一个简单采集功能时我直接使用了file_get_contents函数,但是采集有些网站没问题,采集有些网筹码了,后来分析出现乱码是服务器开启了gzip压缩功能哦。

我采集的一个页面,如下gzip

知道原因了我们就好办了,先百度了一下得出是可以改用curl操作。

curl解决

 代码如下 复制代码

function curl_get($url, $gzip=false){
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
        if($gzip) curl_setopt($curl, CURLOPT_ENCODING, "gzip"); // 关键在这里
        $content = curl_exec($curl);
        curl_close($curl);
        return $content;
}

采用gzip编码格式

file_get_contents解决:

 代码如下 复制代码

file_get_contents("compress.zlib://".$url);

无论页面是否经过gzip压缩,上述代码都可以正常工作!

注意:CURL是需要打开的哦。

curl安装:

xp下面的安装

:修改php.ini文件的设置,找到

 代码如下 复制代码
php_curl.dll

//取消下在的注释extension=php_curl.dll

linux下面安装:

 代码如下 复制代码

# wget http://curl.haxx.se/download/curl-7.17.1.tar.gz

# tar zxvf curl-7.17.1.tar.gz  //解压

#cd curl-7.17.1

# ./configure –prefix=/usr/local/curl

# make

# make install

这是安装php之前安装的方法.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version