乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      file_get_contents高級(jí)用法

       明天網(wǎng)吧 2015-04-03

      file_get_contents高級(jí)用法

      首先解決file_get_contents的超時(shí)問(wèn)題,在超時(shí)返回錯(cuò)誤後就象js中的settimeout那樣進(jìn)行一次嘗試,錯(cuò)誤超過(guò)3次或者5次後就確認(rèn)為無(wú)法連線伺服器而徹底放棄。
      這裡就簡(jiǎn)單介紹兩種解決方法:

      一、增加超時(shí)的時(shí)間限制

      注意:set_time_limit只是設(shè)定你的PHP程式的超時(shí)時(shí)間,而不是file_get_contents函數(shù)讀取URL的超時(shí)時(shí)間。
      我一開始以為set_time_limit也能影響到file_get_contents,後來(lái)經(jīng)測(cè)試是無(wú)效的。真正的修改file_get_contents延時(shí)可以用resource $context的timeout參數(shù):
      PHP程式碼

          $opts = array(
              'http'=>array(
                  'method'=>"GET",
                  'timeout'=>60,
              )
          );

          $context = stream_context_create($opts);

          $html =file_get_contents('http://www.', false, $context);
          fpassthru($fp);

      二、多次嘗試

      PHP程式碼
          $cnt=0;
          while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE){
            $cnt++;
          }

      以上方法對(duì)付超時(shí)已經(jīng)OK了。接下來(lái)演示一下用file_get_contents實(shí)現(xiàn)Post,如下:
      PHP程式碼

          function Post($url, $post = null){
              $context = array();
              if (is_array($post)) {
                  ksort($post);

                  $context['http'] = array (
                      'timeout'=>60,
                      'method' => 'POST',
                      'content' => http_build_query($post, '', '&'),
                   );
              }

              return file_get_contents($url, false, stream_context_create($context));
          }

          $data = array (
              'name' => 'test',
              'email' => 'test@gmail.com',
              'submit' => 'submit',
           );

           echo Post('http://www.', $data);

      注意檔案頭的Set_time_out否則整個(gè)檔案都得超時(shí)了

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多