星期三, 5月 23, 2012

php array轉xml, json

轉json就沒什麼好說的了,想說轉xml應該也是很簡單的東西,不過還真遇到一些問題
  1. array to xml
    原本在google了一段code後,後來發現有中文xml會掛掉因為有不合法字元,
    後來找到以下的code有編碼過,才知道原是編碼的問題
    function toXml($data = array(), $structure = NULL, $basenode = 'xml'){
       // turn off compatibility mode as simple xml throws a wobbly if you don't.
       if (ini_get('zend.ze1_compatibility_mode') == 1)
       {
          ini_set('zend.ze1_compatibility_mode', 0);
       }
    
       if ($structure == NULL)
       {
       $structure = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$basenode />");
       }
    
       // loop through the data passed in.
       $data = $this->_force_loopable($data);
       foreach ($data as $key => $value)
       {
          // no numeric keys in our xml please!
          if (is_numeric($key))
          {
             // make string key...
             //$key = "item_". (string) $key;
             $key = "item";
          }
    
          // replace anything not alpha numeric
          $key = preg_replace('/[^a-z_]/i', '', $key);
    
          // if there is another array found recrusively call this function
          if (is_array($value) OR is_object($value))
          {
             $node = $structure->addChild($key);
             // recrusive call.
             $this->_format_xml($value, $node, $basenode);
          }
          else
          {
            // Actual boolean values need to be converted to numbers
            is_bool($value) AND $value = (int) $value;
    
            // add single node.
            $value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, "UTF-8");
    
             $UsedKeys[] = $key;
    
             $structure->addChild($key, $value);
          }
       }
    
       // pass back as string. or simple xml object if you want!
       return $structure->asXML();
    }
    
    function _force_loopable($data){
       // Force it to be something useful
       if ( ! is_array($data) AND ! is_object($data))
       {
          $data = (array) $data;
       }
    
    return $data;
    }
    
  2. 下header
    browser才會當成xml,而不是html
    $array = array(...);
    header('Content-type: application/xml;charset=utf-8');
    exit(toXml($array)) ;
    

結合一下兩者,利用同一個function回覆
//給header用
$_supported_formats = array(
                'xml' => 'application/xml',
                'rawxml' => 'application/xml',
                'json' => 'application/json',
                'jsonp' => 'application/javascript',
                'serialize' => 'application/vnd.php.serialized',
                'php' => 'text/plain',
                'html' => 'text/html',
                'csv' => 'application/csv'
        );


function response($data, $format = 'json'){
    header('Content-type: '.$this->_supported_formats[$format]);
    switch (strtolower($format)){
        case 'xml':                                
                exit($this->toXml($data));
        case 'json':
        default:
            exit(json_encode($data));
    }
}

沒有留言: