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 Jul 12, 2018 at 9:08
  • Did you create customScript Class in your test.php? Jul 12, 2018 at 10:26
  • No. I have only above code in test.php. Jul 12, 2018 at 10:47
  • Ok what else file you have created? Jul 12, 2018 at 10:49
  • I have updated both files. Jul 12, 2018 at 10:54

4 Answers 4

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
  • Welcome. Happy to Help! Jul 12, 2018 at 11:00
3

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? Aug 23, 2018 at 15:53
0

Use the below code to run a custom script on Magento 2 root folder.

<?php
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$securedArea = $objectManager->get('\Magento\Framework\Registry');
$securedArea->register('isSecureArea', true);

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

$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productCollection = $productCollectionFactory->create();
$productCollection->addAttributeToSelect('image');
$productCollection 
    ->setPageSize(20) // only get 10 products 
    ->setCurPage(1)  // first page (means limit 0,10)
    ->load(); 

foreach($productCollection as $product){
    echo $sku = $product->getSku();echo "<br>";
        
}
?>

If the pub folder is your Magento's root folder then use the following custom script and you need to create it inside pub diectory.

<?php
use Magento\Framework\Config\ConfigOptionsListConstants;
require __DIR__ . '/../app/bootstrap.php';

$objectManagerFactory = \Magento\Framework\App\Bootstrap::createObjectManagerFactory(BP, []);

$objectManager = $objectManagerFactory->create([]);

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

$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productCollection = $productCollectionFactory->create();
$productCollection->addAttributeToSelect('image');
$productCollection 
    ->setPageSize(20) // only get 10 products 
    ->setCurPage(1)  // first page (means limit 0,10)
    ->load(); 

foreach($productCollection as $product){
    echo $sku = $product->getSku();echo "<br>";
}   
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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