Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Paste Description for Web Service Encoded for PHP

Takes arbitrary data and encodes it to be returned (usually as a web service)

Web Service Encoded for PHP
Wednesday, July 8th, 2009 at 5:29:16pm MDT 

  1. <?php
  2. // PHP Web Service Encoder
  3. //
  4. // Takes arbitrary data and encodes it to be returned (usually as a web service)
  5. //
  6. // Data Formats Supported:
  7. // - XML
  8. // - JSON
  9. // - JSONP (JSON with Padding - Used for Direct <script> Callbacks)
  10. //
  11. // Known Bugs:
  12. // - None
  13. //
  14. // Example:
  15. //
  16. /*
  17.         $dataSet = array
  18.         (
  19.                 'persons' => array
  20.                 (
  21.                         array
  22.                         (
  23.                                 'name' => 'Matan Lurey',
  24.                                 'age'  => 20,
  25.                                 'days' => array
  26.                                 (
  27.                                         1, 3, 5, 7
  28.                                 )
  29.                         )
  30.                 )
  31.         );
  32.        
  33.         print WebServiceEncoder::encodeData($dataSet);
  34. */
  35. // Some Notes:
  36. // - XML is not 'well made' to handle data structures. That being said, Array types (in PHP)
  37. // are encoded as best as possible.
  38. //
  39. // array(1, 2, 3) would be
  40. // <item>1</item><item>2</item><item>3</item>
  41. //
  42. // By Matan Lurey
  43. // matan [a t] lurey [d o t] org
  44. //////////////////////////////////////////////////////////////////////////////////////////////
  45. final class WebServiceEncoder
  46. {
  47.         private static function encodeArrayAsXml($parent, $array)
  48.         {
  49.                 foreach ($array as $item)
  50.                 {
  51.                         $childElement = $parent->addChild('item');
  52.                         self::encodeValueAsXml($childElement, $item);
  53.                 }
  54.         }
  55.        
  56.         private static function encodeDataAsJson($data)
  57.         {
  58.                 return json_encode($data);
  59.         }
  60.        
  61.         private static function encodeDataAsJsonP($data, $callbackFunction)
  62.         {
  63.                 return $callbackFunction . '(' . self::encodeDataAsJson($data) . ');';
  64.         }
  65.        
  66.         private static function encodeDataAsXml($data)
  67.         {
  68.                 $rootElement = new SimpleXMLElement('<web-service />');
  69.                 self::encodeValueAsXml($rootElement, $data);
  70.                
  71.                 return $rootElement->asXML();
  72.         }
  73.        
  74.         private static function encodeObjectAsXml($parent, $object)
  75.         {
  76.                 foreach ($object as $key => $value)
  77.                 {
  78.                         $childElement = $parent->addChild($key);
  79.                         self::encodeValueAsXml($childElement, $value);
  80.                 }
  81.         }
  82.        
  83.         private static function encodeValueAsXml($parent, $value)
  84.         {
  85.                 switch (gettype($value))
  86.                 {
  87.                         case 'array':
  88.                                 if (self::isAssocArray($value))
  89.                                         self::encodeObjectAsXml($parent, $value);
  90.                                 else
  91.                                         self::encodeArrayAsXml($parent, $value);
  92.                                 break;
  93.                         case 'object':
  94.                                 self::encodeObjectAsXml($parent, $value);
  95.                                 break;
  96.                         default:
  97.                                 $parent[0] = $value;
  98.                                 break;
  99.                 }
  100.         }
  101.        
  102.         /**
  103.          * @param $data php data to be encoded
  104.          * @param $format type of formatting to be applied
  105.          * @param $optional for jsonp only, the callback function name
  106.          * @return string
  107.          */
  108.         public function encodeData($data, $format = 'xml', $optional = 'webService')
  109.         {
  110.                 switch ($format)
  111.                 {
  112.                         case 'jsonp':
  113.                                 return self::encodeDataAsJsonP($data, $optional);
  114.                         case 'json':
  115.                                 return self::encodeDataAsJson($data);
  116.                         case 'xml':
  117.                                 return self::encodeDataAsXml($data);
  118.                 }
  119.                
  120.                 return null;
  121.         }
  122.        
  123.         private static function isAssocArray($array)
  124.         {
  125.                 return (is_array($array) &&
  126.                         0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
  127.         }
  128. }

Paste Details

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right