Magento – External access to Magento instances

extensionsmagento-1.7magento-communitymodulezend-framework

I have started investigating alternatives to my project and a few questions came out that I couldn't answer by myself.

The problem is: I want to create a web page able to access multiple Magento instances installed on the same server. Currently, I have one Magento instance per client and this project will access several Magento instances to export reports from each one (for example).

The alternatives I thought til this moment are:

  • Have another Magento instance, create a new module within it that changes its 'database target' before triggering operations/queries;

Questions until this moment:

  • Can I 'change the database target' of a Magento instance?
  • How can I access data from a Magento instance without appeal to SOAP/REST?
  • I want to re-use some components (grids, tabs, forms..) from Magento, that's why I'm not considering an independent project (Zend, for instance) that can access this code from another project. Does it make sense?
  • Any other idea?

==Edited==

Thanks by the tips and sorry by my ignorance. The comments let me believe that I'm able to execute something like this:

// File myScript.php
require '/home/DOMAIN1/app/Mage.php';
Mage::app('default');
// get some products from DOMAIN1

require '/home/DOMAIN2/app/Mage.php';
Mage::app('default');
// get some products from DOMAIN2

Is it right? Can I execute require twice (and override things from first require)?

Best Answer

You can create one php script at root folder of your project (Eg. script.php) and add below code to access magento instances:

<?php

error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);   
ini_set('display_errors', 1);
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

/**Here you can add your code to access magento instances**/
?>

To change connection runtime you can use below steps:

Open Magento/app/etc/config.xml add your database configuration in it.

Eg:

<wp_setup>
    <connection>
    <host>< ![CDATA[hostname]]></host>
    <username>< ![CDATA[username]]></username>
    <password>< ![CDATA[password]]></password>
    <dbname>< ![CDATA[wordpress]]></dbname>
    <model>mysql4</model>
    <initstatements>SET NAMES utf8</initstatements>
    <type>pdo_mysql</type>
    <active>1</active>
    </connection>
</wp_setup>
<wp_write>
    <connection>
    <use>wp_setup</use>
    </connection>
</wp_write>
<wp_read>
    <connection>
    <use>wp_setup</use>
    </connection>
</wp_read>

You can access wordpress databse in above script like:

<?php
$read = Mage::getSingleton('core/resource')->getConnection('wp_read');
$write = Mage::getSingleton('core/resource')->getConnection('wp_write');
?>

You can review from here: http://blog.decryptweb.com/connect-database-magento/