In php, file_get_contents obtains webpage garbled characters. Solution:

Source: Internet
Author: User

The following gzip is a page I collected:

If you know the reason, we can do it. Baidu first finds that you can use curl instead.

Curl solution

The code is as follows: Copy code

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"); // The key here
$ Content = curl_exec ($ curl );
Curl_close ($ curl );
Return $ content;
}

Use gzip encoding format

File_get_contents solution:

The code is as follows: Copy code

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

The above code works normally regardless of whether the page is compressed by gzip!

Note: CURL must be enabled.

Curl installation:

Installation Under xp

: Modify the settings of the php. Ini file and find

The code is as follows: Copy code
Php_curl.dll

// Cancel the comment extension = php_curl.dll

Install it in linux:

The code is as follows: Copy code

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

# Tar zxvf curl-7.17.1.tar.gz // Unzip

# Cdcurl-7.17.1

#./Configure-prefix =/usr/local/curl

# Make

# Make install

This is the method installed before installing php.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.