Skip to content

Instantly share code, notes, and snippets.

@xcommerce-gists
Created October 1, 2012 00:20
Show Gist options
  • Save xcommerce-gists/3808807 to your computer and use it in GitHub Desktop.
Save xcommerce-gists/3808807 to your computer and use it in GitHub Desktop.
Step 1 in the x.commerce demo application
F3::route('POST /cart_order',
function() {
//gets the schema URI, parses it
$json_schema = Web::http('GET '.F3::get('SESSION.cart_uri'));
error_log($json_schema);
$avro_schema = AvroSchema::parse($json_schema);
$datum_writer = new AvroIODatumWriter($avro_schema);
$write_io = new AvroStringIO();
$encoder = new AvroIOBinaryEncoder($write_io);
$ch = curl_init();
//get around php magic quotes if needed
//for JSON_decode to work, encode the message body
if(get_magic_quotes_gpc()){
$d = stripslashes($_POST['json_message']);
}else{
$d = $_POST['json_message'];
}
$message = json_decode($d, true);
$datum_writer->write($message, $encoder);
//gets the topic on which the message is sent
curl_setopt($ch, CURLOPT_URL, F3::get('SESSION.cart_topic') );
// pure evil! just for the sake of this example.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//sets method (POST) and headers for the request
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-Type: avro/json"
,"Authorization: ".F3::get('SESSION.cart_token')
,"X-XC-MESSAGE-GUID-CONTINUATION: "
//the next two are the ones you can generate for this guide
,"X-XC-WORKFLOW-ID: " . F3::get('wf_id')
,"X-XC-TRANSACTION-ID: " . F3::get('transaction_id')
,"X-XC-SCHEMA-VERSION: " . F3::get('SESSION.cart_ver')));
//send the curl POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $write_io->string());
$response = curl_exec($ch);
echo $response;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment