PHP Classes
elePHPant
Icontem

Hacker Earth API: Check code of a given language using Hacker Earth

Recommend this page to a friend!
  Info   View files View files (17)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-10-10 (1 year ago) RSS 2.0 feedNot enough user ratingsTotal: 68 All time: 9,447 This week: 279Up
Version Licenses PHP version Categories
hackerearth-api 1.0Artistic License, C...5PHP 5, Emulators
Description Author

This class can check code of a given language using Hacker Earth.

It takes code string or a file with code in a given language and sends a HTTP request to the Hacker Earth API to check the code.

The class returns the API call return values.

Innovation Award
PHP Programming Innovation award nominee
November 2017
Number 3
One way to prevent shipping code with bugs is to test it if would compile and run.

This package can be used to test code in many languages using the HackerHearth API.

Manuel Lemos
Name: Ankit Jain <contact>
Classes: 4 packages by
Country: India India
Innovation award
Innovation award
Nominee: 1x

 

Details

HackerEarth API

Latest Stable Version Build Status Coverage Status Packagist

This package is using HackerEarth api to Compile and Run the code.

HackerEarth Code Checker API. Extremely simple REST API. Supports more than a dozen languages. All powered by reliable HackerEarth servers. You can use your own scoring system or build your own online judge.

Installation

Run this command in your terminal from your project directory:

composer require ankitjain28may/hackerearth-api

Laravel Configuration

When the download is complete, you have to call this package service in config/app.php config file. To do that, add this line in app.php in providers array:

Ankitjain28may\HackerEarth\HackerEarthServiceProvider::class,

To use facade you have to add this line in app.php to the aliases array:

'HackerEarth' => Ankitjain28may\HackerEarth\Facades\HackerEarth::class,

Now run this command in your terminal to publish this package resources:

php artisan vendor:publish --provider="Ankitjain28may\HackerEarth\HackerEarthServiceProvider"
php artisan vendor:publish --tag=migrations

after publishing your config file then open config/hackerearth.php and add your hackerearth app key:

return [
    /*
    |--------------------------------------------------------------------------
    | HackerEarth API KEY
    |--------------------------------------------------------------------------
    |
    | https://api.hackerearth.com/v3/code/
	| https://api.hackerearth.com/v3/code/
    |
    */
    'api_key' => env('HACKEREARTH_SECRET_KEY', 'CLIENT_SECRET_KEY'),
];

also you can add api key in .env :

 HACKEREARTH_SECRET_KEY = YOUR_HACKER_EARTH_API_KEY

Thats it.

API List

    $data = [
        "lang" => '',
        "source" => '',
        "input" => '',
        "async" => 0,                   // default (1 => async req and 0 => sync req)
        "callback" => '',
        'id' => '',
        'time_limit'    => 5,           // default
        'memory_limit'  => 262144,      // default
    ]

  • Run([$data, ..])
  • RunFile([$data, ..])
  • Compile([$data, ..])
  • CompileFile([$data, ..])

Asynchronous Request

  • Set async = 1.
  • You need to add the callback url, Output will be returned directly to the callback url as a post request.

Synchronous Request

  • Set async = 0.
  • Output will be returned with the request's response.

For Core PHP Usage

  • create the database ` create database [database name] `
  • import table ` mysql -u[user] -p[password] [database name] < vendor\ankitjain28may\hackerearth-api\src\Database\migrate.sql `
use Ankitjain28may\HackerEarth\HackerEarth;

$config = [
    	"api_key"     => 'hackerearth_app_key',
    ];


 $hackerearth = new HackerEarth($config);

 $data = [
    "lang" => 'php',
    "source" => '<?php echo "hello World!"; ?>'
 ];

 $result = $hackerearth->Compile([$data]);

 var_dump($result);

 $result = $hackerearth->Run([$data]);

 var_dump($result);

 // Asynchronous

 $data = [
    "lang" => 'php',
    "source" => '<?php echo "hello World!"; ?>',
    "async" => 1,
    "callback" => 'http://callback_url',
    "id" => 12  // Id from the db where to save or update response
 ]

 $result = $hackerearth->Run([$data]);

 vardump($result);

 // Response at Callback URL will need to save to DB with reference to the ID, Id returned is encoded using `bin2hex` which can be decoded using `hex2bin`.

For Laravel Usage

### Code Compile


 use Ankitjain28may\HackerEarth\Facades\HackerEarth;
 use Ankitjain28may\HackerEarth\Models\Output;
 //..
 //..

 $data = [
    "lang" => 'php',
    "source" => '<?php echo "hello World!"; ?>'
 ];

 $result = HackerEarth::Compile([$data, ..]);

 dd($result);

 // Asynchronous

 $data = [
    "lang" => 'php',
    "source" => '<?php echo "hello World!"; ?>',
    "async" => 1,
    "callback" => 'http://callback_url',
    "id" => 12  // Id from the db where to save or update response
 ]

 $result = $hackerearth->Compile([$data]);

 dd($result);

  OR

 Output::saveResult(json_decode($result, True)); // Save directly to the DB


 // Response at Callback URL will save to DB with reference to the ID

 Output::savePayload(json_decode($_POST['payload'], True));

### Code Run


 use Ankitjain28may\HackerEarth\Facades\HackerEarth;
 use Ankitjain28may\HackerEarth\Models\Output;

 //..
 //..

 $data = [
    "lang" => 'php',
    "source" => '<?php echo "hello World!"; ?>'
 ];

 $result = HackerEarth::Run([$data, ..]);

 dd($result);

 // Asynchronous

 $data = [
    "lang" => 'php',
    "source" => '<?php echo "hello World!"; ?>',
    "async" => 1,
    "callback" => 'http://callback_url',
    "id" => 12  // Id from the db where to save or update response
 ]

 $result = $hackerearth->Run([$data]);

 dd($result);

  OR

 Output::saveResult(json_decode($result, True)); // Save directly to the DB


 // Response at Callback URL will save to DB with reference to the ID

 Output::savePayload(json_decode($_POST['payload'], True));

## Also Compile and Run files by passing realpath of the uploaded file--

 use Ankitjain28may\HackerEarth\Facades\HackerEarth;

 //..
 //..

 $data = [
    "lang" => 'php',
    "source" => realpath("test.txt")
 ];

 $result = HackerEarth::RunFile([$data]);
 $result = HackerEarth::CompileFile([$data]);

## Contribute

>Feel free to contribute

License

>Copyright (c) 2017 Ankit Jain - Released under the MIT License

  Files folder image Files  
File Role Description
Files folder imageconfig (1 file)
Files folder imagesrc (2 files, 5 directories)
Files folder imagetests (2 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  config  
File Role Description
  Accessible without login Plain text file hackerearth.php Aux. Auxiliary script

  Files folder image Files  /  src  
File Role Description
Files folder imageCode (2 files)
Files folder imageDatabase (1 file)
Files folder imageFacades (1 file)
Files folder imagemigrations (1 file)
Files folder imageModels (1 file)
  Plain text file HackerEarth.php Class Class source
  Plain text file HackerEarthServiceProvider.php Class Class source

  Files folder image Files  /  src  /  Code  
File Role Description
  Plain text file Compile.php Class Class source
  Plain text file Run.php Class Class source

  Files folder image Files  /  src  /  Database  
File Role Description
  Accessible without login Plain text file migrate.sql Data Auxiliary data

  Files folder image Files  /  src  /  Facades  
File Role Description
  Plain text file HackerEarth.php Class Class source

  Files folder image Files  /  src  /  migrations  
File Role Description
  Plain text file 2017_07_10_182418_...h_outputs_table.php Class Class source

  Files folder image Files  /  src  /  Models  
File Role Description
  Plain text file Output.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file HackerEarthCompileTest.php Class Class source
  Plain text file HackerEarthRunTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:68
This week:0
All time:9,447
This week:279Up