How to input and return data from bootstrap Magento 2 test.php script?












1















I need to pull product data out of Magento 2 via an external PHP command line script.



I am using the example here: How can I bootstrap Magento 2 in a test.php script?



In the run method, I extract my product collection and parse my product data into a new array without any problem.



public function run()
{
// create collection

// parse collection

// build $_products data array

return ['data' => $_products];

}


If I dump or echo $_products out in run() I can see the product data ok, I need to return the product data to the calling function but cannot see how to retrieve the data. I am initiating my app with



$app = $bootstrap->createApplication('Getapp');
$bootstrap->run($app);


How can I access the data returned from Getapp and how can I pass variables into Getapp?










share|improve this question





























    1















    I need to pull product data out of Magento 2 via an external PHP command line script.



    I am using the example here: How can I bootstrap Magento 2 in a test.php script?



    In the run method, I extract my product collection and parse my product data into a new array without any problem.



    public function run()
    {
    // create collection

    // parse collection

    // build $_products data array

    return ['data' => $_products];

    }


    If I dump or echo $_products out in run() I can see the product data ok, I need to return the product data to the calling function but cannot see how to retrieve the data. I am initiating my app with



    $app = $bootstrap->createApplication('Getapp');
    $bootstrap->run($app);


    How can I access the data returned from Getapp and how can I pass variables into Getapp?










    share|improve this question



























      1












      1








      1








      I need to pull product data out of Magento 2 via an external PHP command line script.



      I am using the example here: How can I bootstrap Magento 2 in a test.php script?



      In the run method, I extract my product collection and parse my product data into a new array without any problem.



      public function run()
      {
      // create collection

      // parse collection

      // build $_products data array

      return ['data' => $_products];

      }


      If I dump or echo $_products out in run() I can see the product data ok, I need to return the product data to the calling function but cannot see how to retrieve the data. I am initiating my app with



      $app = $bootstrap->createApplication('Getapp');
      $bootstrap->run($app);


      How can I access the data returned from Getapp and how can I pass variables into Getapp?










      share|improve this question
















      I need to pull product data out of Magento 2 via an external PHP command line script.



      I am using the example here: How can I bootstrap Magento 2 in a test.php script?



      In the run method, I extract my product collection and parse my product data into a new array without any problem.



      public function run()
      {
      // create collection

      // parse collection

      // build $_products data array

      return ['data' => $_products];

      }


      If I dump or echo $_products out in run() I can see the product data ok, I need to return the product data to the calling function but cannot see how to retrieve the data. I am initiating my app with



      $app = $bootstrap->createApplication('Getapp');
      $bootstrap->run($app);


      How can I access the data returned from Getapp and how can I pass variables into Getapp?







      magento2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 7 mins ago









      Utsav Gupta

      15612




      15612










      asked Jul 4 '17 at 13:30









      pajpaj

      2,60521026




      2,60521026






















          1 Answer
          1






          active

          oldest

          votes


















          1














          To return data from run() using the command line external php script example here How can I bootstrap Magento 2 in a test.php script?



          use $app->launch() instead of $bootstrap->run($app) set the response variable and return it



          public function launch()
          {
          $this->_response=$this->run();
          return($this->_response);
          }


          Access the data with



          $mydata=$app->launch();


          You can also save the data in a class variable in run()



           $this->_mydata=$data


          And access it via $app



          $mydata=$app->_mydata


          To make variables available to run() add the variables in an array to the bootstrap construct



              $app = $this->__bootstrap->createApplication(
          'Magento',
          array(
          'variables' => array(
          'product_id' => '65')
          )
          );


          Add this to the "AbstractApp"



          abstract class AbstractApp implements AppInterface
          {
          public function __construct(
          MagentoFrameworkObjectManagerInterface $objectManager,
          EventManager $eventManager,
          AreaList $areaList,
          RequestHttp $request,
          ResponseHttp $response,
          ConfigLoaderInterface $configLoader,
          State $state,
          Filesystem $filesystem,
          MagentoFrameworkRegistry $registry,
          array $variables =
          ) {

          $this->_objectManager = $objectManager;
          $this->_eventManager = $eventManager;
          $this->_areaList = $areaList;
          $this->_request = $request;
          $this->_variables=$variables;
          $this->_response = $response;
          $this->_configLoader = $configLoader;
          $this->_state = $state;
          $this->_filesystem = $filesystem;
          $this->registry = $registry;
          }


          Access the variable array in run()



          $product_id=$this->_variables['product_id'];





          share|improve this answer

























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f181996%2fhow-to-input-and-return-data-from-bootstrap-magento-2-test-php-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            To return data from run() using the command line external php script example here How can I bootstrap Magento 2 in a test.php script?



            use $app->launch() instead of $bootstrap->run($app) set the response variable and return it



            public function launch()
            {
            $this->_response=$this->run();
            return($this->_response);
            }


            Access the data with



            $mydata=$app->launch();


            You can also save the data in a class variable in run()



             $this->_mydata=$data


            And access it via $app



            $mydata=$app->_mydata


            To make variables available to run() add the variables in an array to the bootstrap construct



                $app = $this->__bootstrap->createApplication(
            'Magento',
            array(
            'variables' => array(
            'product_id' => '65')
            )
            );


            Add this to the "AbstractApp"



            abstract class AbstractApp implements AppInterface
            {
            public function __construct(
            MagentoFrameworkObjectManagerInterface $objectManager,
            EventManager $eventManager,
            AreaList $areaList,
            RequestHttp $request,
            ResponseHttp $response,
            ConfigLoaderInterface $configLoader,
            State $state,
            Filesystem $filesystem,
            MagentoFrameworkRegistry $registry,
            array $variables =
            ) {

            $this->_objectManager = $objectManager;
            $this->_eventManager = $eventManager;
            $this->_areaList = $areaList;
            $this->_request = $request;
            $this->_variables=$variables;
            $this->_response = $response;
            $this->_configLoader = $configLoader;
            $this->_state = $state;
            $this->_filesystem = $filesystem;
            $this->registry = $registry;
            }


            Access the variable array in run()



            $product_id=$this->_variables['product_id'];





            share|improve this answer






























              1














              To return data from run() using the command line external php script example here How can I bootstrap Magento 2 in a test.php script?



              use $app->launch() instead of $bootstrap->run($app) set the response variable and return it



              public function launch()
              {
              $this->_response=$this->run();
              return($this->_response);
              }


              Access the data with



              $mydata=$app->launch();


              You can also save the data in a class variable in run()



               $this->_mydata=$data


              And access it via $app



              $mydata=$app->_mydata


              To make variables available to run() add the variables in an array to the bootstrap construct



                  $app = $this->__bootstrap->createApplication(
              'Magento',
              array(
              'variables' => array(
              'product_id' => '65')
              )
              );


              Add this to the "AbstractApp"



              abstract class AbstractApp implements AppInterface
              {
              public function __construct(
              MagentoFrameworkObjectManagerInterface $objectManager,
              EventManager $eventManager,
              AreaList $areaList,
              RequestHttp $request,
              ResponseHttp $response,
              ConfigLoaderInterface $configLoader,
              State $state,
              Filesystem $filesystem,
              MagentoFrameworkRegistry $registry,
              array $variables =
              ) {

              $this->_objectManager = $objectManager;
              $this->_eventManager = $eventManager;
              $this->_areaList = $areaList;
              $this->_request = $request;
              $this->_variables=$variables;
              $this->_response = $response;
              $this->_configLoader = $configLoader;
              $this->_state = $state;
              $this->_filesystem = $filesystem;
              $this->registry = $registry;
              }


              Access the variable array in run()



              $product_id=$this->_variables['product_id'];





              share|improve this answer




























                1












                1








                1







                To return data from run() using the command line external php script example here How can I bootstrap Magento 2 in a test.php script?



                use $app->launch() instead of $bootstrap->run($app) set the response variable and return it



                public function launch()
                {
                $this->_response=$this->run();
                return($this->_response);
                }


                Access the data with



                $mydata=$app->launch();


                You can also save the data in a class variable in run()



                 $this->_mydata=$data


                And access it via $app



                $mydata=$app->_mydata


                To make variables available to run() add the variables in an array to the bootstrap construct



                    $app = $this->__bootstrap->createApplication(
                'Magento',
                array(
                'variables' => array(
                'product_id' => '65')
                )
                );


                Add this to the "AbstractApp"



                abstract class AbstractApp implements AppInterface
                {
                public function __construct(
                MagentoFrameworkObjectManagerInterface $objectManager,
                EventManager $eventManager,
                AreaList $areaList,
                RequestHttp $request,
                ResponseHttp $response,
                ConfigLoaderInterface $configLoader,
                State $state,
                Filesystem $filesystem,
                MagentoFrameworkRegistry $registry,
                array $variables =
                ) {

                $this->_objectManager = $objectManager;
                $this->_eventManager = $eventManager;
                $this->_areaList = $areaList;
                $this->_request = $request;
                $this->_variables=$variables;
                $this->_response = $response;
                $this->_configLoader = $configLoader;
                $this->_state = $state;
                $this->_filesystem = $filesystem;
                $this->registry = $registry;
                }


                Access the variable array in run()



                $product_id=$this->_variables['product_id'];





                share|improve this answer















                To return data from run() using the command line external php script example here How can I bootstrap Magento 2 in a test.php script?



                use $app->launch() instead of $bootstrap->run($app) set the response variable and return it



                public function launch()
                {
                $this->_response=$this->run();
                return($this->_response);
                }


                Access the data with



                $mydata=$app->launch();


                You can also save the data in a class variable in run()



                 $this->_mydata=$data


                And access it via $app



                $mydata=$app->_mydata


                To make variables available to run() add the variables in an array to the bootstrap construct



                    $app = $this->__bootstrap->createApplication(
                'Magento',
                array(
                'variables' => array(
                'product_id' => '65')
                )
                );


                Add this to the "AbstractApp"



                abstract class AbstractApp implements AppInterface
                {
                public function __construct(
                MagentoFrameworkObjectManagerInterface $objectManager,
                EventManager $eventManager,
                AreaList $areaList,
                RequestHttp $request,
                ResponseHttp $response,
                ConfigLoaderInterface $configLoader,
                State $state,
                Filesystem $filesystem,
                MagentoFrameworkRegistry $registry,
                array $variables =
                ) {

                $this->_objectManager = $objectManager;
                $this->_eventManager = $eventManager;
                $this->_areaList = $areaList;
                $this->_request = $request;
                $this->_variables=$variables;
                $this->_response = $response;
                $this->_configLoader = $configLoader;
                $this->_state = $state;
                $this->_filesystem = $filesystem;
                $this->registry = $registry;
                }


                Access the variable array in run()



                $product_id=$this->_variables['product_id'];






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jul 5 '17 at 8:40

























                answered Jul 5 '17 at 8:05









                pajpaj

                2,60521026




                2,60521026






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f181996%2fhow-to-input-and-return-data-from-bootstrap-magento-2-test-php-script%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Magento 2 controller redirect on button click in phtml file

                    Polycentropodidae