Curling a URL in PHP

Assume the form object contains an array in the form of params

 $fieldString = http_build_query($form->getParams());

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $this->_dotMailerUri);
curl_setopt($ch,CURLOPT_POST, count($form->getParams()));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fieldString);
// Set this so the result isn't added to stdout and gets returned instead
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

Programatically add a Layout Update File in Magento

We sometimes need logic to decide whether an update file should be loaded or not, and need to do this outside of a module’s config.xml area/layout/update node. Instead, we can use an observer and programatically decide whether to add the layout file or not.

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Module>
            <version>1.0.0</version>
        </Namespace_Module>
    </modules>
    <global>
        <models>
            <mymodule>
                <class>Loewenstark_Layout_Model</class>
            </mymodule>
        </models>
        <events>
            <core_layout_update_updates_get_after>
                <observers>
                    <mymodule_add_layout>
                        <type>singleton</type>
                        <class>mymodule/observer</class>
                        <method>addLayoutXml</method>
                    </mymodule_add_layout>
                </observers>
            </core_layout_update_updates_get_after>     
        </events>
    </global>
</config>

The Observer

Because the updates object extends a SimpleXML Object, we can add our file to it here

<?php
class Namespace_Module_Model_Observer
{
    public function loadXML(Varien_Event_Observer $observer)
    {
        // Conditional to decide whether to load the XML file
        if(Mage::helper('mymodule')->isEnabled()) {
            $xml = $observer->getUpdates()
                ->addChild('mymodule');
            /* @var $xml SimpleXMLElement */
            $xml->addAttribute('module', 'Namespace_Module');
            $xml->addChild('file', 'mymodule/layoutfile.xml');
        }
    }
}