How to solve the problem that php uses the file_get_contents method to grab web page data and appears garbled

零到壹度
Release: 2023-03-21 21:02:01
Original
3218 people have browsed it

We all often encounter garbled characters when capturing data, which makes people collapse. Today I will mainly discuss with you how to solve the problem of garbled webpage data captured by PHP using the file_get_contents method. Friends who need it can refer to it. I hope it can help everyone. Let’s take a look with the editor below.

Method 1:

## Will $data=file_get_contents ($url); change to $data=file_get_contents("compress.zlib://".$url);.

The reason is that the content of the webpage is GZIP compressed.

Method 2:

Use curl Method capture,

Define a function

 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;   
}
Copy after login

Then call it, $data = curl_get ($url,false);

Related recommendations:

Two solutions to using file_get_contents to grab garbled web pages

php file_get_contents grabs the garbled content

php file_get_contents gets the garbled code caused by gzip encoding of the web content

The above is the detailed content of How to solve the problem that php uses the file_get_contents method to grab web page data and appears garbled. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!