/** * 提供基于flash的AMF3格式的数据返回 * @static * @author 欧远宁 * @param array $data 返回的数据 */ public static function amf($data) { if (!extension_loaded('amf')) { throw new err('need amf ext'); } header('Content-Type: application/x-amf'); echo substr(amf_encode($data, AMF_AMF3 | AMF_BIG_ENDIAN | AMFC_ARRAY | AMFR_ARRAY), 1); }
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"; } }
<?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 { }
/** * undocumented function * * @return void * **/ function __encodeHeader($value) { $result = amf_encode($value, 1); return $result; }
/** * serialize is the run method of the class. When serialize is called * the AMFObject passed in is read and converted into the amf binary * representing the PHP data represented. * * @param object $d the AMFObject to serialize */ function serialize(&$amfout) { $encodeCallback = array(&$this, "encodeCallback"); $this->writeInt(0); // write the version ??? $count = $amfout->numOutgoingHeader(); $this->writeInt($count); // write header count for ($i = 0; $i < $count; $i++) { //write headers $header =& $amfout->getOutgoingHeaderAt($i); $this->writeUTF($header->name); $this->writeByte(0); $tempBuf = $this->outBuffer; $this->outBuffer = ""; if ($this->native) { $this->outBuffer .= amf_encode($header->value, $this->encodeFlags, $encodeCallback); } else { $this->writeData($header->value); } $tempBuf2 = $this->outBuffer; $this->outBuffer = $tempBuf; $this->writeLong(strlen($tempBuf2)); $this->outBuffer .= $tempBuf2; } $count = $amfout->numBody(); $this->writeInt($count); // write the body count for ($i = 0; $i < $count; $i++) { //write body $this->amf0StoredObjects = array(); $this->storedStrings = array(); $this->storedObjects = array(); $this->encounteredStrings = 0; $this->storedDefinitions = 0; $body =& $amfout->getBodyAt($i); $this->currentBody =& $body; $this->writeUTF($body->responseURI); // write the responseURI header $this->writeUTF($body->responseTarget); // write null, haven't found another use for this $tempBuf = $this->outBuffer; $this->outBuffer = ""; if ($this->native) { $this->outBuffer .= amf_encode($body->getResults(), $this->encodeFlags, $encodeCallback); } else { $this->writeData($body->getResults()); } $tempBuf2 = $this->outBuffer; $this->outBuffer = $tempBuf; $this->writeLong(strlen($tempBuf2)); $this->outBuffer .= $tempBuf2; } return $this->outBuffer; }
<?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); } catch (Exception $e) { http_response_code(500); var_dump($e); die; die('Invalid AMF packet'); }
function __encode($value) { if (function_exists('amf_encode')) { return amf_encode($value, 1); } if (is_object($value)) { return 'doh, encoding not done. get AMFEXT'; } return $value; }