[PHP] file_get_contents,stream_context_creat →→→→→进入此内容的聊天室

来自 , 2020-05-29, 写在 PHP, 查看 98 次.
URL http://www.code666.cn/view/29969626
  1. <?php
  2.     /**
  3.      * to do 使用file_get_contents,以及stream_context_create获取远程接口,并增加重试
  4.      * param string $tongjiUrl
  5.      * return string  
  6.      */
  7.     public function getTongjiUrl($tongjiUrl) {
  8.         $optTongji  = array(
  9.             'http'  => array(
  10.                 'method'  => "GET",
  11.                 'timeout' => 60,
  12.             )
  13.         );
  14.         $context  = stream_context_create($optTongji);
  15.         $i = 0;
  16.         while($i++ < 5) {
  17.             $tongjiRet = file_get_contents($tongjiUrl,false,$context);
  18.             if (empty($tongjiRet)) {
  19.                 CLogger::notice("get url return false for $i time!");
  20.                 usleep(2000);
  21.                 continue;
  22.             }
  23.             return $tongjiRet;
  24.         }
  25.     }
  26.  

回复 "file_get_contents,stream_context_creat"

这儿你可以回复上面这条便签

captcha