Skip to content

Instantly share code, notes, and snippets.

@xcommerce-gists
Created August 10, 2012 20:57
Show Gist options
  • Save xcommerce-gists/3317829 to your computer and use it in GitHub Desktop.
Save xcommerce-gists/3317829 to your computer and use it in GitHub Desktop.
gist for Magento example
<?php
//
// This module is part of the Ponger capability. The module:
// -- handles the Ping message of the PingCapability transaction of the PingPong workflow
// -- sends the Pong message of the PingCapability transaction of the PingPong workflow in response
//
// Copy to your <magento-install-root> directory
include_once 'lib/avro.php';
// include_once 'common.php';
/// Open the log file to which to write outputs
$fp = fopen('Avro_Coder.log', 'at');
// Get all http headers in the received message
$headers = getallheaders();
// Get the posted message body
// NOTE: The message body is currently in Avro binary form
$post_data = file_get_contents("php://input");
// Get the URI of the Avro schema on the OCL server that adheres to the /cse/offer/create contract
$schema_uri = $headers['X-XC-SCHEMA-URI'];
// Get the contents of the Avro schema identified by the URI retrieved above
$content = file_get_contents($schema_uri);
// Parse the CSE Avro schema and place results in an AvroSchema object
$schema = AvroSchema::parse($content);
//fwrite($fp, $schema);
//fwrite($fp, "\n");
// Use Avro to decode and deserialize the binary-encoded message body.
// The result is the plain text version of the message body
// The message sender used Avro to binary-encode the text version of the message body before sending the message.
// Create an AvroIODatumReader object for the supplied AvroSchema.
// An AvroIODatumReader object handles schema-specific reading of data from the decoder and
// ensures that each datum read is consistent with the reader's schema.
$datum_reader = new AvroIODatumReader($schema);
// Create an AvroStringIO object and assign it the encoded message body
$read_io = new AvroStringIO($post_data);
// Create an AvroIOBinaryDecoder object and assign it the $read_io object
$decoder = new AvroIOBinaryDecoder($read_io);
// Decode and deserialize the data using the CSE schema and the supplied decoder
// The data is retrieved from the AvroStringIO object $read_io created above
// Upon return, $message contains the plain text version of the X.commerce message sent by the publisher
$message = $datum_reader->read($decoder);
//fwrite($fp, $post_data);
fwrite($fp, "\n");
//fwrite($fp, print_r($message, true));
fwrite($fp, print_r($headers,true));
// Write to log file
fwrite($fp, print_r($message, true));
fwrite($fp,"Inserted document with ID: " . $item['_id']);
fclose($fp);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment