2

Below url throwing Class customScript does not exist error. my test.php and customScript.php are in test folder. Once i run http://127.0.0.1/magento2/test.php, getting "Class customScript does not exist error" .

Here is my test.php code.

<?php

  require __DIR__ . '../../app/bootstrap.php';

 $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

 $app = $bootstrap->createApplication('customScript');

$bootstrap->run($app);

And in customScript.php

<?php

  class customScript extends \Magento\Framework\App\Http implements 

   \Magento\Framework\AppInterface {

public function launch()
{
    $this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
    $id = 11588;
    $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);

    echo $_product->getName();

    return $this->_response;
}

public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
    return false;
}


  }

How do I run php custom script on magento2

Please anyone able to achieve this?

16
  • paste the content of test.php – Sukumar Gorai Jul 12 '18 at 9:08
  • Did you create customScript Class in your test.php? – Sukumar Gorai Jul 12 '18 at 10:26
  • No. I have only above code in test.php. – jafar pinjar Jul 12 '18 at 10:47
  • Ok what else file you have created? – Sukumar Gorai Jul 12 '18 at 10:49
  • I have updated both files. – jafar pinjar Jul 12 '18 at 10:54
2

Here is the code you need to use for test.php

<?php    
require DIR . '../../app/bootstrap.php';
require dirname(__FILE__) . '/customScript.php';

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

$app = $bootstrap->createApplication('customScript');

$bootstrap->run($app);
1
2

Please try below code to run external script in magento2 :

<?php
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';
$params =  $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

echo "test custom script";

?>
1

Use below code to run a custom script from Magento 2 root folder

<?php
    use Magento\Framework\App\Bootstrap;

    include('app/bootstrap.php');
    require dirname(__FILE__) . '/customScript.php';
    $bootstrap = Bootstrap::create(BP, $_SERVER);

    $objectManager = $bootstrap->getObjectManager();

    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');

    $app = $bootstrap->createApplication('customScript');

    $bootstrap->run($app);
1
  • shall we execute any class of different modules by including the path? – jafar pinjar Aug 23 '18 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.