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
- <?php
- // PHP Web Service Encoder
- //
- // Takes arbitrary data and encodes it to be returned (usually as a web service)
- //
- // Data Formats Supported:
- // - XML
- // - JSON
- // - JSONP (JSON with Padding - Used for Direct <script> Callbacks)
- //
- // Known Bugs:
- // - None
- //
- // Example:
- //
- /*
- $dataSet = array
- (
- 'persons' => array
- (
- array
- (
- 'name' => 'Matan Lurey',
- 'age' => 20,
- 'days' => array
- (
- 1, 3, 5, 7
- )
- )
- )
- );
- print WebServiceEncoder::encodeData($dataSet);
- */
- // Some Notes:
- // - XML is not 'well made' to handle data structures. That being said, Array types (in PHP)
- // are encoded as best as possible.
- //
- // array(1, 2, 3) would be
- // <item>1</item><item>2</item><item>3</item>
- //
- // By Matan Lurey
- // matan [a t] lurey [d o t] org
- //////////////////////////////////////////////////////////////////////////////////////////////
- final class WebServiceEncoder
- {
- {
- foreach ($array as $item)
- {
- $childElement = $parent->addChild('item');
- self::encodeValueAsXml($childElement, $item);
- }
- }
- {
- return json_encode($data);
- }
- {
- return $callbackFunction . '(' . self::encodeDataAsJson($data) . ');';
- }
- {
- $rootElement = new SimpleXMLElement('<web-service />');
- self::encodeValueAsXml($rootElement, $data);
- return $rootElement->asXML();
- }
- {
- foreach ($object as $key => $value)
- {
- $childElement = $parent->addChild($key);
- self::encodeValueAsXml($childElement, $value);
- }
- }
- {
- {
- case 'array':
- if (self::isAssocArray($value))
- self::encodeObjectAsXml($parent, $value);
- else
- self::encodeArrayAsXml($parent, $value);
- break;
- case 'object':
- self::encodeObjectAsXml($parent, $value);
- break;
- default:
- $parent[0] = $value;
- break;
- }
- }
- /**
- * @param $data php data to be encoded
- * @param $format type of formatting to be applied
- * @param $optional for jsonp only, the callback function name
- * @return string
- */
- public function encodeData($data, $format = 'xml', $optional = 'webService')
- {
- switch ($format)
- {
- case 'jsonp':
- return self::encodeDataAsJsonP($data, $optional);
- case 'json':
- return self::encodeDataAsJson($data);
- case 'xml':
- return self::encodeDataAsXml($data);
- }
- return null;
- }
- {
- }
- }
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.
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.