歡迎來到小豬圈!

2007-12-30

關於 php_curl (二)

  • 以下的 $ch 指的是 php curl_init() 傳回的 resource(cURL handle) ,同等於 libcurl 的 easy-handle,$mh 以此類推。
  • $ch 在加入 $mh 之後就不能重覆使用(意指僅用 curl_setopt() 重設 URL)了,得關掉後重新初始化再來。
  • $ch 在加入 $mh 之後 curl_errno() 會失效(總是傳回 int(0),原因不明),而 curl_error() 則是有些錯誤抓不到(已知 CURLE_COULDNT_RESOLVE_HOST(6) 不行)。
  • 承上, 改用 curl_multi_info_read() 替代。傳回值如下:
    <?php
      $curlmsg = curl_multi_info_read($mh);
      var_dump( $curlmsg );
      // 當沒有訊息時傳回
      boolean(false)
      // 反之則傳回
      array(3) {
        ["msg"]=>
        int(1)
        ["result"]=>
        int(0)
        ["handle"]=>
        resource(9) of type (curl)
      }
    ?>
    
  • $curlmsg["msg"] 目前只有定義 CURLMSG_DONE(1) 一值,暫時可以不用理它。
  • $curlmsg["result"] 值為 CURLE_* 之一。
  • $curlmsg["handle"] 訊息來源的 $ch。
  • curl_multi_info_read() 較為明確的呼叫時機,是當執行 curl_multi_exec($mh, $rh) 之後, $rh 變動(減少)的時候。
  • curl_multi_select() 並非必要的,呼叫的主要目地是為了先檢查開啟的 sockets 有無資料,再做對應的動作,以減少佔用的 CPU 資源。以下為 Unix Socket FAQ 摘錄:
      2.9.  What are the pros/cons of select(), non-blocking I/O and SIGIO?
    
      Using non-blocking I/O means that you have to poll sockets to see if
      there is data to be read from them.  Polling should usually be avoided
      since it uses more CPU time than other techniques.
    
      Using SIGIO allows your application to do what it does and have the
      operating system tell it (with a signal) that there is data waiting
      for it on a socket.  The only drawback to this soltion is that it can
      be confusing, and if you are dealing with multiple sockets you will
      have to do a select() anyway to find out which one(s) is ready to be
      read.
    
      Using select() is great if your application has to accept data from
      more than one socket at a time since it will block until any one of a
      number of sockets is ready with data.  One other advantage to select()
      is that you can set a time-out value after which control will be
      returned to you whether any of the sockets have data for you or not.
    
  • [comp.unix.programmer] Unix-socket-faq for network programming

No comments:

Post a Comment

Comment Form Message

標籤分類

Blog Archive

Labels

Google Analytics Tracking Code

About Me

My photo
Keelung, R.O.C, Taiwan
一個不學無術、混吃等死的傢伙…