奶头挺立呻吟高潮av全片,成人试看120秒体验区,性欧美极品v,A片高潮抽搐揉捏奶头视频

php語言

php中file-get-contents與curl性能比較分析

時間:2025-05-21 07:14:18 php語言 我要投稿
  • 相關(guān)推薦

php中file-get-contents與curl性能比較分析

  在php中如果不仔細的去分析性能會發(fā)現(xiàn)file_get_contents與curl兩個同很多共同點的,他們都可以采集文件打開文件,但是如果仔細一對比會發(fā)現(xiàn)很多不同點,以下是小編為大家搜索整理的php中file_get_contents與curl性能比較分析, 希望能給大家?guī)韼椭?更多精彩內(nèi)容請及時關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

  PHP中fopen,file_get_contents,curl函數(shù)的區(qū)別:

  1.fopen /file_get_contents 每次請求都會重新做DNS查詢,并不對 DNS信息進行緩存。但是CURL會自動對DNS信息進行緩存。對同一域名下的網(wǎng)頁或者圖片的請求只需要一次DNS查詢。這大大減少了DNS查詢的次數(shù)。所以CURL的性能比fopen /file_get_contents 好很多。

  2.fopen /file_get_contents 在請求HTTP時,使用的是http_fopen_wrapper,不會keeplive。而curl卻可以。這樣在多次請求多個鏈接時,curl效率會好一些。

  3.fopen / file_get_contents 函數(shù)會受到php.ini文件中allow_url_open選項配置的影響。如果該配置關(guān)閉了,則該函數(shù)也就失效了。而curl不受該配置的影響。

  4.curl 可以模擬多種請求,例如:POST數(shù)據(jù),表單提交等,用戶可以按照自己的需求來定制請求。而fopen / file_get_contents只能使用get方式獲取數(shù)據(jù)。

  file_get_contents 獲取遠程文件時會把結(jié)果都存在一個字符串中 fiels函數(shù)則會儲存成數(shù)組形式

  因此,我還是比較傾向于使用curl來訪問遠程url。Php有curl模塊擴展,功能很是強大。

  說了半天大家可能說性能怎么沒對比呢,那我們就來看看

  最近需要獲取別人網(wǎng)站上的音樂數(shù)據(jù)。用了file_get_contents函數(shù),但是總是會遇到獲取失敗的問題,盡管按照手冊中的 例子設(shè)置了超時,可多數(shù)時候不會奏效:

  $config['context'] = stream_context_create(array('http' => array('method' => "GET",

  'timeout' => 5//這個超時時間不穩(wěn)定,經(jīng)常不奏效

  )

  ));

  這時候,看一下服務(wù)器的連接池,會發(fā)現(xiàn)一堆類似的錯誤,讓我頭疼萬分:

  file_get_contents(http://***): failed to open stream…

  現(xiàn)在改用了curl庫,寫了一個函數(shù)替換:

  function curl_file_get_contents($durl){

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $durl);

  curl_setopt($ch, CURLOPT_TIMEOUT, 5);

  curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);

  curl_setopt($ch, CURLOPT_REFERER,_REFERER_);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  $r = curl_exec($ch);

  curl_close($ch);

  return $r;

  }

  如此,除了真正的網(wǎng)絡(luò)問題外,沒再出現(xiàn)任何問題。

  這是別人做過的關(guān)于curl和file_get_contents的測試:

  file_get_contents抓取google.com需用秒數(shù):

  2.31319094

  2.30374217

  2.21512604

  3.30553889

  2.30124092

  curl使用的時間:

  0.68719101

  0.64675593

  0.64326

  0.81983113

  0.63956594

  差距很大?呵呵,從我使用的經(jīng)驗來說,這兩個工具不只是速度有差異,穩(wěn)定性也相差很大。

  建議對網(wǎng)絡(luò)數(shù)據(jù)抓取穩(wěn)定性要求比較高的朋友使用上面的 curl_file_get_contents函數(shù),不但穩(wěn)定速度快,還能假冒瀏覽器欺騙目標地址哦

  再看一個實例

  后續(xù)貼出了curl和file_get_contents的對比結(jié)果,這邊除了curl與file_get_contents的性能對比,還包含了他們的性能對比,講之前看下如下的結(jié)果圖:

  curl與file_get_contents性能對比PHP源代碼如下:

  /**

  * 通過淘寶IP接口獲取IP地理位置

  * @param string $ip

  * @return: string

  **/

  function getCityCurl($ip)

  {

  $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;

  $ch = curl_init();

  $timeout = 5;

  curl_setopt ($ch, CURLOPT_URL, $url);

  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

  $file_contents = curl_exec($ch);

  curl_close($ch);

  $ipinfo=json_decode($file_contents);

  if($ipinfo->code=='1'){

  return false;

  }

  $city = $ipinfo->data->region.$ipinfo->data->city;

  return $city;

  }

  function getCity($ip)

  {

  $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;

  $ipinfo=json_decode(file_get_contents($url));

  if($ipinfo->code=='1'){

  return false;

  }

  $city = $ipinfo->data->region.$ipinfo->data->city;

  return $city;

  }

  // for file_get_contents

  $startTime=explode(' ',microtime());

  $startTime=$startTime[0] + $startTime[1];

  for($i=1;$i<=10;$i++)

  {

  echo getCity("121.207.247.202")."

  ";

  }

  $endTime = explode(' ',microtime());

  $endTime = $endTime[0] + $endTime[1];

  $totalTime = $endTime - $startTime;

  echo 'file_get_contents:'.number_format($totalTime, 10, '.', "")." seconds

  ";

  //for curl

  $startTime2=explode(' ',microtime());

  $startTime2=$startTime2[0] + $startTime2[1];

  for($i=1;$i<=10;$i++)

  {

  echo getCityCurl('121.207.247.202')."

  ";

  }

  $endTime2 = explode(' ',microtime());

  $endTime2=$endTime2[0] + $endTime2[1];

  $totalTime2 = $endTime2 - $startTime2;

  echo "curl:".number_format($totalTime2, 10, '.', "")." seconds";

  ?>

  測試訪問

  http://www.jb51.net

  file_get_contents速度:4.2404510975 seconds

  curl速度:2.8205530643 seconds

  curl比file_get_contents速度快了30%左右,最重要的是服務(wù)器負載更低.

  總結(jié)

  file_get_contents處理頻繁小的時候,用它感覺挺好的。沒什么異常。如果你的文件被1k+人處理。那么你的服務(wù)器cpu就等著高升吧。所以建議自己和大家在以后寫php代碼的時候使用curl庫。

【php中file-get-contents與curl性能比較分析】相關(guān)文章:

php中file-get-contents與curl性能分析10-04

php中file-get-contents與curl性能比較08-13

php的file-get-contents與curl性能分析09-04

php中的socket框架性能分析07-17

PHP中CURL的幾個經(jīng)典應(yīng)用08-12

淺談php中curl和fsockopen的應(yīng)用08-26

PHP前端開發(fā)中的性能05-25

php中的curl使用入門教程06-20

分析PHP性能調(diào)優(yōu)實戰(zhàn)09-14

主站蜘蛛池模板: 山阴县| 石狮市| 武夷山市| 靖州| 象山县| 桃园县| 喀喇沁旗| 弥勒县| 鹤岗市| 凤冈县| 松溪县| 龙南县| 若尔盖县| 东乌珠穆沁旗| 通榆县| 惠来县| 南投市| 安丘市| 呼和浩特市| 关岭| 石景山区| 彰武县| 襄汾县| 治多县| 元阳县| 韶山市| 都匀市| 确山县| 蕲春县| 清苑县| 泽普县| 甘洛县| 威海市| 苏尼特右旗| 海门市| 和政县| 新乡县| 枣强县| 中方县| 拜泉县| 鹤庆县|