Reputation: 461
I created a custom module in Magento that creates a variable with an array of order information anytime an order is placed. I now need to create some sort of mechanism or procedure that sends these variables to an external database. However, I'm not sure which route to take. Can someone point me in the right direction?
EDIT: Here's what I have so far for the Observer.php file:
<?php
class Companyname_Dataport_Model_Observer
{
public function orderPaid(Varien_Event_Observer $observer)
{
$orderId = $observer->getPayment()->getOrder()->getId();
$orderdata = array();
$orderdata['id'] = $order->getIncrementId();
$orderdata['customername'] = $order->getCustomerFirstname() . " " . $order->getCustomerLastname();
$orderdata['price'] = $order->getPrice();
$orderdata['items'] = array();
foreach ($order->getAllItems() as $orderItem) {
$item = array();
$item['productid'] = $orderItem->getProductId();
$item['qty'] = $orderItem->getQtyOrdered();
$orderdata['items'][] = $item;
}
// Writing to the DOM
}
}
Upvotes: 1
Views: 1430
Reputation: 5654
you can use cURL.... directly write the code in your observer.php. you will need the followung things while writing the curl....
Your data would be sent in json format.... Here is a sample sender example:
$xurl='http://domain/Observer_test/';//open bravo or any other application's endpoint here
$curl = curl_init();
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $xurl);
curl_setopt($curl, CURLOPT_USERPWD, "111:abc");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array("data" => json_encode($data)));
$resp = curl_exec($curl);
curl_close($curl);
$resp = json_decode($resp);
And the sample receiver code in php(can be in any language):
$userkey=$_SERVER["PHP_AUTH_USER"];
$usersecret=$_SERVER["PHP_AUTH_PW"];
$key = '111';//16 character
$secret = 'abc';//32 character
if(!($userkey==$key && $usersecret==$secret)) {
http_response_code(403);
}else{
$data = $_POST["data"];
$dir='/path/to/save/file';
$file = new SplFileObject($dir."fread.txt", "w");
$written = $file->fwrite($data);
if($written){
header('Content-Type: application/json');
http_response_code(200);
echo json_encode(array("stat"=>"SUCCESS"));
}
}
So basically it is a web service following REST architecture
Upvotes: 1
Reputation: 8587
I recomend you to use an observer, listening to an event related to order creation.
For example, you could listen to sales_order_save_after
and in the observer your method will take as parameter $observer
. You can then access the order data with $order = $observer->getEvent()->getOrder();
.
Here is some code:
config.xml:
<config>
...
<global>
...
<events>
<sales_order_save_after>
<observers>
<some_descriptive_string>
<type>singleton</type>
<class>themodelalias/observer</class>
<method>theMethodToUse</method>
</some_descriptive_string>
</observers>
</sales_order_save_after>
</events>
...
</global>
...
</config>
Observer.php:
class Yournamespace_Yourmodule_Model_Observer
{
public function theMethodToUse($observer)
{
$order = $observer->getEvent()->getOrder();
//here goes your code to send the information to your external db
{
}
Upvotes: 1
Reputation: 5410
You can go around this several ways, here are two of them that I would go for:
1) Easiest: at the moment where the order is placed and you have the information, you can use PHP exec() function to call a standalone PHP file. (Where the variables are passed ofcourse)
Example:
exec("php /path/to/standalone/script.php ". $variable);
(You might need to json_encode $variable if it's an array)
This script just used the variables and connects to a local/remote database to run a query and update the information.
2) You could create a web service on the external server that can be called whenever you want from Magento
There are still other ways to do this but I hope my answer contains useful information already that can get you started.
Upvotes: 1