WordPress gets the remote file content and displays it

» WordPress » WordPress gets the remote file content and displays it

Using curl extension, theme developers can push information to theme/plug-in users, which is definitely a practical technology

WordPress获取远程文件内容并显示出来-极客公园

preface

During the theme development yesterday, I had an idea that we should communicate with the theme users, or push some information to the users, such as the use skills of the theme. So Baidu found that the website has a similar solution. I made a new one and successfully implanted the current theme of the site. Let's share it by the way.

introduce

The method to obtain remote file content is mainly to use fopen / file_get_contents / curl Three methods, but fopen / file_get_contents DNS query will be performed again for each request, and DNS information will not be cached. however CURL The DNS information is automatically cached. The request for a web page or image under the same domain name requires only one DNS query. This greatly reduces the number of DNS queries.

So the performance of CURL is much better than that of fopen/file_get_contents

Code usage

 <? php $url = " http://git.oschina.net/yunluo/API/raw/master/notice.txt "; $ch = curl_init(); curl_setopt ($ch,  CURLOPT_URL, $url); curl_setopt ($ch,  CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch,  CURLOPT_CONNECTTIMEOUT,10); $notice = curl_exec($ch); echo $notice; ?>

use

It's OK to create a txt text locally on your server. I build it in Git, and then the theme automatically gets the text. After the update here, the client will immediately change under the refresh.

Actual effect

WordPress获取远程文件内容并显示出来-极客公园

--End--

-- 完 --

Post reply

Your email address will not be disclosed. Required items have been used * tagging

6 Replies to "WordPress gets the remote file content and displays it"