PHP code example of redberry / spear

1. Go to this page and download the library: Download redberry/spear library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

redberry / spear example snippets


  
  
  use Redberry\Spear\Facades\Spear;
  
  $nodeCode = "console.log('hello Spear!')";
  
  $data = Spear::node()->execute($nodeCode);
  
  dump($data->toArray());
  /**
   * [
   *  'result_code'   => 0,
   *  'error_message' => null,
   *  'output'        => 'hello Spear!',
   * ]
   */

  
  
  use Redberry\Spear\Facades\Spear;
  
  $nodeCode = <<<END
    let data = '';
    
    const solve = () => {
      data = data.trim();
      console.log(`hello, ${data}`);
    }
    
    process.stdin.on('data', c => data += c);
    process.stdin.on('end', solve);
   END;
  
  $data = Spear::node()->execute($nodeCode, 'Speeeear');
  
  dump($data->toArray());
  /**
   * [
   *  'result_code'   => 0,
   *  'error_message' => null,
   *  'output'        => 'hello, Speeeear',
   * ]
   */