


The garbled problem when reading remote files with php file_get_contents is caused by gzip compression.
Yesterday I found that the weather api of China Weather Network that I made before was saved locally and I found that some cities were garbled.
I still can’t find the reason. Because it looks completely normal in the browser. like. Read Yinchuan City's weather for the day http://m.weather.com.cn/data/101170101.html
and view its json data in the browser. It is completely normal. Encoding is also normal. But using file_get_contents to read the output in the browser is garbled.
<span>$url</span> = 'http://m.weather.com.cn/data/101170101.html'<span>; </span><span>echo</span> '<pre class="brush:php;toolbar:false">'<span>; </span><span>print_r</span>(<span>file_get_contents</span>(<span>$url</span>));
I was busy online for a while and found out the reason: China Weather Network turned on gzip compression. Found a solution from http://www.php10086.com/2012/03/516.html
PHP's file_get_contents gets the remote page content. If it is gzip encoded, the returned string is encoded garbled code. How? There are two ways to solve the gzip problem:
curl solution:
<span>function</span> curl_get(<span>$url</span>, <span>$gzip</span>=<span>false</span><span>){ </span><span>$curl</span> = curl_init(<span>$url</span><span>); curl_setopt(</span><span>$curl</span>, CURLOPT_RETURNTRANSFER, 1<span>); curl_setopt(</span><span>$curl</span>, CURLOPT_CONNECTTIMEOUT, 10<span>); </span><span>if</span>(<span>$gzip</span>) curl_setopt(<span>$curl</span>, CURLOPT_ENCODING, "gzip"); <span>//</span><span> 关键在这里</span><span>$content</span> = curl_exec(<span>$curl</span><span>); curl_close(</span><span>$curl</span><span>); </span><span>return</span><span>$content</span><span>; }</span>
Use gzip encoding format
file_get_contents solution :
<span>file_get_contents</span>("compress.zlib://".<span>$url</span>);
No matter the page The above code works fine with or without gzip compression!
Supported by PHP 4.3.0 and later, and can also be used for functions like fopen~!
Solution:
<span>$url</span> = 'http://m.weather.com.cn/data/101170101.html'<span>; </span><span>echo</span> '<pre class="brush:php;toolbar:false">'<span>; </span><span>print_r</span>(<span>file_get_contents</span>("compress.zlib://".<span>$url</span>));<span>//</span><span>打开gzip压缩过的页面。 路径前不加compress.zlib:// 打开会有乱码。 </span>
The above introduces the garbled problem of php file_get_contents reading remote files caused by gzip compression, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


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

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

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools
