PHP WDDX: Simple Serialization Using PHP function


Web Distributed Data Exchange Step By Step tutorial - Part 8: Now, we try to serialize data use PHP's function. PHP have 2 function to serialize data into WDDX packet, wddx_serialize_value() wddx_serialize_vars(). these functions create a complete WDDX packet using just single line. oke, look following sample:
<?
$arrNum = array(1,2,3);

print wddx_serialize_value($arrNum, "This is my first wddx packet");

?>

The result is like this:

<wddxPacket version='1.0'>
  <header>
    <comment>This is my first wddx packet</comment>
  </header>
  <data>
    <array length='3'>
      <number>1</number>
      <number>2</number>
      <number>3</number>
    </array>
  </data>
</wddxPacket>

Another option to serialize data using wddx_serialize_vars(). We can put more than one variabel in this function, but we can not put header. This is example:

<?php
$arrNum = array(1,2,3);
$arrStr = array('PHP Undercover','Wiwit Siswoutomo');
$arr    = array($arrNum, $arrStr);


print wddx_serialize_vars('arrNum','arrStr','arr');

?>

The result:

<wddxPacket version='1.0'>
  <header/>
  <data>
    <struct>
      <var name='arrNum'>
        <array length='3'>
          <number>1</number>
          <number>2</number>
          <number>3</number>
        </array>
      </var>
      <var name='arrStr'>
        <array length='2'>
          <string>PHP Undercover</string>
          <string>Wiwit Siswoutomo</string>
        </array>
      </var>
      <var name='arr'>
        <array length='2'>
          <array length='3'>
            <number>1</number>
            <number>2</number>
            <number>3</number>
          </array>
          <array length='2'>
            <string>PHP Undercover</string>
            <string>Wiwit Siswoutomo</string>
          </array>
        </array>
      </var>
    </struct>
  </data>
</wddxPacket>


Series this article:
PHP WDDX: Introduction
PHP WDDX: Understanding Anatomy of WDDX
PHP WDDX: Understanding Data Elements
PHP WDDX: Understanding Simple Data Type
PHP WDDX: Using Array Elements
PHP WDDX: Struct Element
PHP WDDX: Recordset Element
PHP WDDX: Simple Serialization Using PHP function
PHP WDDX: Complex Serialization
PHP WDDX: Unserializing Data
PHP WDDX: Creating Web Service Server
PHP WDDX: Creating Web Service Client

Bookmark and Share Tag: wddx. distributed data, serialization, wddx_serialize_value Category: PHP Application Post : April 30th 2008 Read: 21,168

advertisements


blog comments powered by Disqus