歡迎來到小豬圈!

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

2007-12-27

廣告插播? - HiNet 線上通知服務同意書

2007-12-25

關於 php_curl

  • 最近開發 Stylet Reader 時,為了要抓取 feed 用上了 php_curl,但文件實在有夠少的。雖然 libcurl 官網有一份 C API 的文件,但命名有些出入,所以我自製了一份對照表(見文末,僅供參考)。
  • 從原始碼看來 curl_multi_select() 僅是代為呼叫 curl_multi_fdset() 與 select() ,後者可參考socket_select()
  • 依照文字說明, curl_multi_info_read() 的第二個參數($msgs_in_queue)應該是 passing by reference 吧!?怎麼會是 passing by value 呢?真詭異。
  • 承上,雖然可以手動傳址 curl_multi_info_read( $mh, &$msgs_in_queue),但得打開 allow_call_time_pass_reference = On (不建議)。
  • select Function (Windows) MSDN。
php_curl 與 libcurl 的函數對應
php_curl libcurl
curl_close curl_easy_cleanup
curl_copy_handle curl_easy_duphandle
curl_errno
curl_error curl_easy_strerror
curl_exec curl_easy_perform
curl_getinfo curl_easy_getinfo
curl_init curl_easy_init
curl_multi_add_handle curl_multi_add_handle
curl_multi_close curl_multi_cleanup
curl_multi_exec curl_multi_perform
curl_multi_getcontent
curl_multi_info_read curl_multi_info_read
curl_multi_init curl_multi_init
curl_multi_remove_handle curl_multi_remove_handle
curl_multi_select curl_multi_fdset + select
curl_setopt_array
curl_setopt curl_easy_setopt
curl_version curl_version

2007-12-21

關於 meeya

監獄兔 ウサビッチ|USAVICH.TV

關於 PHP Constants (常數)

  • 關於 PHP 的常數:
    • 在宣告時其值必需為已知、固定的值。
    • 常數名稱在“慣例”上都是大寫,並且字詞間以底線分開。如:define('FOO_BAR', 'something');
    • 宣告該常數以後,在執行期間其值不能被改變。
    • 不管在哪宣告,全域都能存取。
  • 使用常數的目的之一是要決解“魔術數字”的問題,即程式碼中沒有註解或命名的數字,容易造成程式碼不易維護。
  • 使用常數時,效能不該是考量的要素之一,因為在 PHP 使用常數“並沒有”比用變數快!(這倒是出乎我的預料)剛試了一下,用常數反而慢一點點(差距很小,不曉得是不是我測試方式的關係)。
  • 以下為我測試的程式碼,註解的地方為可選的比較方式:
    <pre>
    <?php
    define('VAR1' , 'a long long long long long long long long long long long long long text');
    define('VAR2' , 'a long long long long long long long long long long long long long text');
    $var1 = 'a long long long long long long long long long long long long long text';
    $var2 = 'a long long long long long long long long long long long long long text';
    
    $start = 0;
    $end = 0;
    
    for($i =0; $i < 20; $i++){
      $tmp = null;
      $start = microtime(true);
      for($j =0; $j < 5000; $j++){
        $tmp .= $var1;
        //$tmp = "Something here" . $var1;
        //$tmp = "Something here{$var1} Nothing here{$var2}";
        //$tmp = "Something here{$GLOBALS['var1']} Nothing here{$GLOBALS['var2']}";
      }
      $end = microtime(true);
      echo 'V: ' . ($end - $start);
      echo "\t";
    
      $tmp = null;
      $start = microtime(true);
      for($j =0; $j < 5000; $j++){
        $tmp .= VAR1;
        //$tmp = "Something here" . VAR1;
        //$tmp = 'Something here' . VAR1 . ' Nothing here' . VAR2 ;
      }
      $end = microtime(true);
      echo 'C: ' . ($end - $start);
      echo "\n";
    }
    ?>
    </pre>
    
  • 自行定義的常數值,在宣告時必需為固定的值,如:
    define('TEST_TXT', 'some string here');
    
    但一個實用的例外(來自 wordpress 的 wp-config-sample.php),是用 magic constants 來取得路徑:
    define('ABSPATH', dirname(__FILE__));
    
  • PHP: Constants - Manual
  • Magic number (programming) - Wikipedia, the free encyclopedia

2007-12-20

Yahoo UI 是好物

  • 真的是易學易用,不過速度似乎有點慢…。
  • 事件綁定時要去掉“on”(不知是否我眼挫,這件事我在文件上沒找到?),如:
    window.onload = doSomething;
    // 上述改用 YUI 如下
    YAHOO.util.Event.addListener(window, 'load', doSomething);
    
  • YAHOO.util.Event.addListener 會自動附加(而不是覆蓋)綁定,以前要自已做到這功能的話得如下:
    function addListener(newListener) {
      var oldListener = window.onload;
      if (typeof window.onload != 'function') {
        window.onload = newListener;
      } else {
        window.onload = function() {
          oldListener();
          newListener();
        }
      }
    }
    
  • 範例不錯,但文件不夠完備。像 API Doc 的部份,如果能每個 Method 附上簡短範例會更好(像 PHP Manual 那樣)。

2007-12-17

安裝 VMware 與 CentOS

  • 就為了 PHP: flush - Manual 這一句:

    Several servers, especially on Win32, will still buffer the output from your script until it terminates before transmitting the results to the browser.

  • 看了鳥哥的 Linux 私房菜後決定安裝 CentOS,目前版本是最近才更新的 CentOS 5.1。
  • 初次用文字模式,安裝過程相當順利。第一個遇到的問題是 shutdown 與 Run-level (一裝完就想關機了… 囧)。輸入 shutdown now 的話只會切換到 Run-level 1,要關機的話是要輸入 shutdown -h now 才切換到 Run-level 0 ,意即關機。
  • Linux Run-level 的 7 種模式
    • 0: 關機
    • 1: 單一使用者模式
    • 2: 單一使用者模式附帶網路功能
    • 3: 啟動於文字模式下的多使用者模式
    • 4: 未定義
    • 5: 啟動於 X Windows 模式下的多使用者模式
    • 6: 重開機
  • BTW,我覺得 Linux 令人難已接近的原因之一就是那堆難以聯想的縮寫(指令、檔名、目錄名都是)…。
  • 鳥哥的 Linux 私房菜 -- 伺服器架設篇目錄
  • www.centos.org - The Community ENTerprise Operating System
  • Runlevel - Wikipedia, the free encyclopedia

2007-12-15

(X)HTML content-type 的選擇

  • 這裡說的 Content-type 和 Media type 基本上是一樣的東西,源自於MIME type。其常見的內容項目有:text/html 、 image/gif 、 application/octet-stream … 等。
  • 在 HTTP 協定中會送出 Content-Type 的 header 來指明接下來要傳送的資料類型。如:
    Content-Type: text/html
    
  • XHTML Media Types - Summary 該文總結中指出: HTML 應該使用 text/html , 而 XHTML 應該使用 application/xhtml+xml,但在考量與 HTML 相容的情況下也可以使用 text/html 。
  • IANA | MIME Media Types

2007-12-14

紀曉君與家家演唱的 Santa Lucia

  • 康熙來了(20071127)片段,紀曉君與家家合唱的 Santa Lucia:

2007-12-13

MySQL Index 長度限制為 1000 “bytes”

2007-12-12

2007-12-10

字字珠璣

2007-12-09

初試 Google Code - Project Hosting 提供 svn 服務

2007-12-07

Charset (字元集)的選擇:UTF-8!

  • Just UTF-8!這該算是資訊界難得的共識吧。
  • UTF-8 是一種不固定長度的編碼方式,原定義在 ASCII (128 以下) 佔用 1byte,而其它語言的字元則佔 2、3或4個 bytes。
  • 漢字在 UTF-8 中佔 3 bytes ,相較於用 Big5(字元固定 2 bytes)編碼的文件,其所佔用的空間較多。
  • 時至今日,大多的編輯器都能正確支援 UTF-8。唯一要注意的是:不要寫入 BOM,像是 Windows 內建的 Notepad 在轉存 UTF-8 格式時會自動在檔案頭開插入 EF BB BF ,雖然在支援 UTF-8 編輯器不會顯示出來,但這會造成一些問題(如驗証 HTML 時會出錯)。
  • Byte-order mark - Wikipedia, the free encyclopedia

DTD 的選擇:Strict!

  • DTD Document Type Definition 也就是一般網頁原始碼上<!DOCTYPE開頭的東西。個人推薦的是 XHTML 1.0 StrictHTML 4.01 Strict
  • 無論是選擇 XHTML 或 HTML,都要 Strict!Strict 可不僅僅是宣告,在撰寫時也要避免依賴瀏覽器容錯的能力。
  • 選擇 HTML 4.01 Strict 如 Yahoo! UI LibraryGrids CSS 所推薦的:
    We use and recommend this !DOCTYPE to trigger Standards Mode in browsers with multiple rendering modes:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
    
  • 選擇 XHTML 1.0 Strict 則是在程式處理上比較方便:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
  • HTML 4.01 Specification
  • XHTML™ 1.0 The Extensible HyperText Markup Language (Second Edition)

2007-12-06

Super Pitch Gorugo

  • 日本人對投魔球還真有迷樣的執著啊。

2007-12-05

關於 Hinet 與 Xuite 信箱設定

  • 要使用 Xuite 信箱 POP3 的功能,得先下面鏈結處申請才行。不然會看到 [AUTH] Authentication failed. 的錯誤訊息。
  • 而 Hinet 信箱 會遇到在 Webmail 看是有信的,但收信軟體收不到信的情況。這要到“個人設定 » 進階 » 登出時(強制)移回信件”設定。
  • 申請開放 POP3 服務 - HiNet 會員中心
  • HiNet webmail -常見問題

關於 MagpieRSS

  • 目前最新的版本是 magpierss-0.72 (November 5, 2005),已經是兩年前的東西啦…。
  • 文件不夠詳細。
  • Snoopy.class.inc 需要 socket 的功能,如 fsockopen(),但很多免費空間都不允許。
  • fetch_rss() 函式只能從 URL 載入 Feed。
  • 從檔案載入 Feed 的寫法:
    require_once(MAGPIE_DIR . "rss_fetch.inc");
    
    // 一般用法
    $rss = fetch_rss($url);
    
    // 用 CURL 替代
    $ch = curl_init($url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $xml_string = curl_exec($ch);
    curl_close($ch);
    
    // 或從檔案讀入
    $xml_string = file_get_contents("feed.xml");
    
    // 載入預設值(原本是設計在裡 fetch_rss() 呼叫的)
    init();
    
    $rss = new MagpieRSS( $xml_string, MAGPIE_OUTPUT_ENCODING, MAGPIE_INPUT_ENCODING, MAGPIE_DETECT_ENCODING );
    
    
  • Magpie RSS - PHP RSS Parser

2007-12-04

剛申請了 Google AdSense

2007-12-03

關於 MySQL 的 TIMESTAMP 格式

2007-12-01

RSS 與 ATOM 各版本比對

Name Version Full name root Namespace
RSS 0.90 RDF Site Summary rdf:RDF http://my.netscape.com/rdf/simple/0.9/
RSS 0.91 Rich Site Summary rss
RSS 1 RDF Site Summary rdf:RDF http://purl.org/rss/1.0/
RSS 2 Really Simple Syndication rss
ATOM 0.3 feed http://purl.org/atom/ns
ATOM 1 feed http://www.w3.org/2005/Atom

標籤分類

Blog Archive

Labels

Google Analytics Tracking Code

About Me

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