External access to Magento instances

by MatheusJardimB   Last Updated April 02, 2018 11:09 AM

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)?



Answers 2


If they're on the same server and you have access to SSH it shouldn't be to hard to set this up. Actually I did this before with a little help from my webhosting company

Make sure all the Mage installations and your platform are in the same Linux user group, set permissions of the files up in a way that the group also has read/write(/execute) permissions.

Now you can go ahead and include the Magento Core like you normally would

require /home/domain(1|2|3|...).com/www/app/Mage.php
Mage::app('default');

This will give you access to that specific core. The only catch is that this will work for just one core per time, including multiple cores into one script will result in an endless supply of errors.

I did some poking around in the core but it doesn't look like you can swap different Database connections at runtime so actually connecting to different Magento installation databases is gonna take a lot of customisation I think.

Sander Mangel
Sander Mangel
October 18, 2013 06:47 AM

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/

Bijal Bhavsar
Bijal Bhavsar
October 18, 2013 08:54 AM

Related Questions


Migration from Magento 1 to Magento 2

Updated September 12, 2017 19:09 PM

Delivery date and time based on city

Updated November 15, 2016 08:03 AM

magento2 registration php

Updated January 25, 2018 11:09 AM