curl模塊讀取遠(yuǎn)程文件
在PHP開發(fā)時(shí),讀取文件的時(shí)候,我們想到的第一個(gè)方法是file_get_contents和fopen,但使用這兩個(gè)方法,需要開啟allow_url_fopen,在服務(wù)器上配置開啟了allow_url_fopen,會(huì)存在安全性隱患,所以服務(wù)器建議關(guān)閉allow_url_fopen,那么,在關(guān)閉這個(gè)的情況下,我們該怎樣讀取遠(yuǎn)程文件內(nèi)容呢?
好在PHP提供了curl模塊,我們可以用curl模塊去讀取遠(yuǎn)程文件。
<?php
$ch = curl_init("http://m.lncdfzh.com.cn/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$str = curl_exec($ch); if ($str !== false) { // do something with the content
echo $str;
}
curl_close($ch);?>
會(huì)員登錄
賬號(hào)登錄還沒有賬號(hào)?立即注冊