2012年9月20日 星期四

如何透過PHP傳送、接收XML

To receive XML, try the following code:

To send XML:

                  
                      I like Bharath.co.uk!
                  
                 ';
  // We send XML via CURL using POST with a http header of text/xml.
  $ch = curl_init('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($ch, CURLOPT_REFERER, 'http://www.bharath..co.uk');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $ch_result = curl_exec($ch);
  curl_close($ch);
  // Print CURL result.
  echo $ch_result;
?>
原文:http://www.bharath.co.uk/articles/2011/05/php-sending-and-receiving-xml-using-php

PHP 使用 SimpleXML 來解析 XML 內容、屬性

PHP 可以使用 simplexml_load_string、simplexml_load_file 來解析 XML, 以取得內容. 程式 與 XML 內容

  
    Test
    Jon
    Tsung
  

XML;
 
$xml = simplexml_load_string($string);
print_r($xml);
?>
XML 解析 的 內容會回傳一個物件
SimpleXMLElement Object
(
    [@attributes] => Array
    (
       [responsecode] => 200
    )
 
    [result] => SimpleXMLElement Object
    (
        [@attributes] => Array
        (
            [count] => 10
            [start] => 0
            [totalhits] => 13304
        )
 
       [title] => Test
       [from] => Jon
       [to] => Tsung
    )
)
如何取用此物件回傳的值
取得 result 下的 title
$xml->result->title; // Test (object)
建議: (string)$xml->result->title; // 強迫轉換成字串
取得屬性的值(@attributes)
$xml->result->attributes()->totalhits; // 13304 (object), 一樣建議於前面加 (string)

$result_attr = $xml->result->attributes();
$result_attr['totalhits']; // 13304 (object), 一樣建議於前面加 (string)
原文來源:http://blog.longwin.com.tw/2009/10/php-simplexml-parse-attr-content-2009/

2012年9月13日 星期四

你的PHP程式是否會產生Y2K38的Bug?

原本的時間取得方式會導致Y2K38

用PHP 5.2.0 後的新的DateTime Class來修正
format($format);
?>
不過2038還那麼久, 現在擔心好像還太早, 目前很難出現災情, 不然以後都習慣用DateTime Class就好了 原文:http://whhnote.blogspot.tw/2010/09/php-phpy2k38bug.html

2012年9月11日 星期二

PHP 縮圖

原文:http://webcache.googleusercontent.com/search?q=cache:EeR-EeFI5pUJ:blog.longwin.com.tw/2007/08/php_image_resize_2007/+&cd=1&hl=zh-TW&ct=clnk
/*
 * 抓取要縮圖的比例, 下述只處理 jpeg
 * $from_filename : 來源路徑, 檔名, ex: /tmp/xxx.jpg
 * $save_filename : 縮圖完要存的路徑, 檔名, ex: /tmp/ooo.jpg
 * $in_width : 縮圖預定寬度
 * $in_height: 縮圖預定高度
 * $quality  : 縮圖品質(1~100)
 *
 * Usage:
 *   ImageResize('ram/xxx.jpg', 'ram/ooo.jpg');
 */
function ImageResize($from_filename, $save_filename, $in_width, $in_height, $quality)
{
    $allow_format = array('jpeg', 'png', 'gif');
    $sub_name = $t = '';
    
    // Get new dimensions
    $img_info = getimagesize($from_filename);
    $width    = $img_info['0'];
    $height   = $img_info['1'];
    $imgtype  = $img_info['2'];
    $imgtag   = $img_info['3'];
    $bits     = $img_info['bits'];
    $channels = $img_info['channels'];
    $mime     = $img_info['mime'];

    $in_width = ($in_width == '') ? $width : $in_width;
    $in_height = ($in_height == '') ? $height : $in_height;
    $quality = ($quality == '') ? 100 : $quality;
    
    list($t, $sub_name) = split('/', $mime);
    if ($sub_name == 'jpg') {
        $sub_name = 'jpeg';
    }

    if (!in_array($sub_name, $allow_format)) {
        return false;
    }

    // 取得縮在此範圍內的比例
    $percent = getResizePercent($width, $height, $in_width, $in_height);
    $new_width  = $width * $percent;
    $new_height = $height * $percent;
    /*
     * echo "from_filename = ".$from_filename."
"; * echo "width = ".$width."
"; * echo "height = ".$height."
"; * echo "in_width = ".$in_width."
"; * echo "in_height = ".$in_height."
"; * echo "percent = ".$percent."
"; */ // Resample $image_new = imagecreatetruecolor($new_width, $new_height); // $function_name: set function name // => imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif /* // $sub_name = jpeg, png, gif $function_name = 'imagecreatefrom' . $sub_name; if ($sub_name=='png') return $function_name($image_new, $save_filename, intval($quality / 10 - 1)); $image = $function_name($filename); //$image = imagecreatefromjpeg($filename); */ $image = imagecreatefromjpeg($from_filename); imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return imagejpeg($image_new, $save_filename, $quality); } /** * 抓取要縮圖的比例 * $source_w : 來源圖片寬度 * $source_h : 來源圖片高度 * $inside_w : 縮圖預定寬度 * $inside_h : 縮圖預定高度 * * Test: * $v = (getResizePercent(1024, 768, 400, 300)); * echo 1024 * $v . "\n"; * echo 768 * $v . "\n"; */ function getResizePercent($source_w, $source_h, $inside_w, $inside_h) { if ($source_w < $inside_w && $source_h < $inside_h) { return 1; // Percent = 1, 如果都比預計縮圖的小就不用縮 } $w_percent = $inside_w / $source_w; $h_percent = $inside_h / $source_h; return ($w_percent > $h_percent) ? $h_percent : $w_percent; } ?>

2012年9月9日 星期日

$(function(){}) vs $(document).ready(function(){})

$(function(){}) vs $(document).ready(function(){}) jquery強大的$()使用法,參數可以是一個function,代表這是一個callback,什麼事件的callback呢?就是DOM is ready的callbck,簡單一點說,下面兩個語法是同樣意思的:
$(function(){}) == $(document).ready(function(){})
也等於EXT的
Ext.onReady(function(){});
這裡補充一點 $(document).ready(function($){}) 如果再function中的參數加入「$」,表示在function中的敘述可以很安全的使用$來代表jquery的語法。