国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php中file_get_contents和curl兩個函數(shù)用法

2024-09-04 11:48:36
字體:
供稿:網(wǎng)友

文章簡單的介紹了php中file_get_contents和curl兩個函數(shù)用法,在不能使用file_get_contents時可以嘗試一下curl函數(shù),下面是file_get_contents和curl兩個函數(shù)同樣功能的不同寫法.

file_get_contents函數(shù)的使用示例,代碼如下:

  1. <?php 
  2. $file_contents = file_get_contents('http://www.survivalescaperooms.com/'); 
  3. echo $file_contents
  4. ?> 

換成curl函數(shù)的使用示例,代碼如下:

  1. <?php 
  2. $ch = curl_init(); 
  3. $timeout = 5; 
  4. curl_setopt ($ch, CURLOPT_URL, 'http://www.survivalescaperooms.com'); 
  5. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  6. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
  7. $file_contents = curl_exec($ch); 
  8. curl_close($ch); 
  9. echo $file_contents; 
  10. ?> 

利用function_exists函數(shù)來判斷php是否支持一個函數(shù)可以輕松寫出下面函數(shù),代碼如下:

  1. <?php 
  2.  
  3. function vita_get_url_content($url) { 
  4.  
  5. if(function_exists('file_get_contents')) { 
  6.  
  7. $file_contents = file_get_contents($url); 
  8.  
  9. else { 
  10.  
  11. $ch = curl_init(); 
  12.  
  13. $timeout = 5; 
  14.  
  15. curl_setopt ($ch, CURLOPT_URL, $url); 
  16.  
  17. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  18.  
  19. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
  20.  
  21. $file_contents = curl_exec($ch); 
  22.  
  23. curl_close($ch); 
  24.  
  25.  
  26. return $file_contents
  27.  
  28.  
  29. ?> 

其實上面的這個函數(shù)還有待商議,如果你的主機服務商把file_get_contents和curl都關(guān)閉了,上面的函數(shù)就會出現(xiàn)錯誤.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 普洱| 梁山县| 运城市| 红安县| 尚义县| 瑞丽市| 曲水县| 安吉县| 莒南县| 汤阴县| 桃园县| 于都县| 临澧县| 鄂尔多斯市| 南漳县| 眉山市| 凤台县| 鄂托克前旗| 靖西县| 陆河县| 鹿邑县| 临江市| 罗甸县| 合肥市| 定安县| 兴安盟| 水富县| 尖扎县| 宿松县| 章丘市| 拉孜县| 阳高县| 五华县| 来凤县| 句容市| 凌海市| 祥云县| 元阳县| 墨玉县| 北安市| 阜南县|