function encodeAndDecode($input) { $encoded = amf_encode($input); var_dump(bin2hex($encoded)); $decoded = amf_decode($encoded); if ($input === $decoded) { var_dump($input); echo "OK\n\n"; } else { var_dump($input); echo "Encode/decode created\n"; var_dump($decoded); echo "FAILED\n\n"; } }
/** * readBody converts the payload of the message into php objects. */ function readBody() { $this->body_count = $this->readInt(); // find the total number of body elements while ($this->body_count--) { // loop over all of the body elements $this->amf0storedObjects = array(); $this->storedStrings = array(); $this->storedObjects = array(); $this->storedDefinitions = array(); $target = $this->readUTF(); $response = $this->readUTF(); // the response that the client understands //$length = $this->readLong(); // grab the length of the body element $this->current_byte += 4; if ($this->native) { $data = amf_decode($this->raw_data, $this->decodeFlags, $this->current_byte, array(&$this, "decodeCallback")); } else { $type = $this->readByte(); // grab the type of the element $data = $this->readData($type); // turn the element into real data } $this->amfdata->addBody(new MessageBody($target, $response, $data)); // add the body element to the body object } }
/** * Enter description here... * * @param unknown_type $type * @return unknown */ function __decode($type = null) { $_this =& AmfDispatcher::getInstance(); if (function_exists('amf_decode')) { if (!class_exists('AmfCallback')) { App::import('Vendor', 'Amf.AmfCallback'); } $AmfCallback =& new AmfCallback($this); return amf_decode($_this->input, $_this->bigEndian, $_this->offset, array($AmfCallback, "decode")); } else { if ($type == null) { $type = $_this->__next(); } switch ($type) { case 'date': $stamp = floor($_this->read('float') / 1000); $offset = $_this->read('int'); if ($offset > 720) { $offset = 65536 - $offset; } return $stamp + ($offset * 60 - date('Z')); case 'reference': $id = $_this->read('int'); if (!isset($_this->references[$id])) { return false; } return $_this->references[$id]; case 'array': $data = array(); $length = $_this->read('long'); while ($length--) { $data[] = $_this->__decode(); } return $data; case 'mixed_array': $length = $_this->read('long'); return $_this->__decode('object'); case 'object': case 'typed_object': $data = array(); if ($type == 'typed_object') { $class = $_this->__find('str'); } while (1) { $key = $_this->__find('str'); $type = $_this->read('byte'); if ($type == $_this->types[0]['object_term']) { break; } $data[$key] = $_this->__find($type); } if ($type == 'typed_object') { // eventually use the actual class name here: $class = 'stdObject'; $data = Set::map($data, $class); } $_this->references[] = $data; return $data; case 'amf3': return new AMF3Data($_this->__decode3()); break; } } return $data; }
<?php require_once __DIR__ . '/vendor/autoload.php'; $data = file_get_contents("php://input"); header('Content-Type: application/x-amf'); try { $deserialized = amf_decode($data); http_response_code(200); echo amf_encode($deserialized, AMF_CLASS_MAPPING); } catch (Exception $e) { http_response_code(500); var_dump($e); die; die('Invalid AMF packet'); } class XXX { } class YYY { }