HackerEarth API
, (*1)
This package is using HackerEarth api to Compile and Run the code., (*2)
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., (*3)
, (*4)
PHP Innovation Award for the PHP package Hacker Earth API Certification Link, (*5)
Installation
Run this command in your terminal from your project directory:, (*6)
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:, (*7)
Ankitjain28may\HackerEarth\HackerEarthServiceProvider::class,
To use facade you have to add this line in app.php
to the aliases
array:, (*8)
'HackerEarth' => Ankitjain28may\HackerEarth\Facades\HackerEarth::class,
Now run this command in your terminal to publish this package resources:, (*9)
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:, (*10)
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
:, (*11)
HACKEREARTH_SECRET_KEY = YOUR_HACKER_EARTH_API_KEY
Thats it., (*12)
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 database [database name]
mysql -u[user] -p[password] [database name] < vendor\ankitjain28may\hackerearth-api\src\Database\migrate.sql
```php
use Ankitjain28may\HackerEarth\HackerEarth;, (*13)
$config = [
"api_key" => 'hackerearth_app_key',
];, (*14)
$hackerearth = new HackerEarth($config);, (*15)
$data = [
"lang" => 'php',
"source" => ''
];, (*16)
$result = $hackerearth->Compile([$data]);, (*17)
var_dump($result);, (*18)
$result = $hackerearth->Run([$data]);, (*19)
var_dump($result);, (*20)
// Asynchronous, (*21)
$data = [
"lang" => 'php',
"source" => '',
"async" => 1,
"callback" => 'http://callback_url',
"id" => 12 // Id from the db where to save or update response
], (*22)
$result = $hackerearth->Run([$data]);, (*23)
vardump($result);, (*24)
// 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
., (*25)
```, (*26)
For Laravel Usage
### Code Compile, (*27)
```php, (*28)
use Ankitjain28may\HackerEarth\Facades\HackerEarth;
use Ankitjain28may\HackerEarth\Models\Output;
//..
//.., (*29)
$data = [
"lang" => 'php',
"source" => ''
];, (*30)
$result = HackerEarth::Compile([$data, ..]);, (*31)
dd($result);, (*32)
// Asynchronous, (*33)
$data = [
"lang" => 'php',
"source" => '',
"async" => 1,
"callback" => 'http://callback_url',
"id" => 12 // Id from the db where to save or update response
], (*34)
$result = $hackerearth->Compile([$data]);, (*35)
dd($result);, (*36)
OR, (*37)
Output::saveResult(json_decode($result, True)); // Save directly to the DB, (*38)
// Response at Callback URL will save to DB with reference to the ID, (*39)
Output::savePayload(json_decode($_POST['payload'], True));, (*40)
```, (*41)
### Code Run, (*42)
```php, (*43)
use Ankitjain28may\HackerEarth\Facades\HackerEarth;
use Ankitjain28may\HackerEarth\Models\Output;, (*44)
//..
//.., (*45)
$data = [
"lang" => 'php',
"source" => ''
];, (*46)
$result = HackerEarth::Run([$data, ..]);, (*47)
dd($result);, (*48)
// Asynchronous, (*49)
$data = [
"lang" => 'php',
"source" => '',
"async" => 1,
"callback" => 'http://callback_url',
"id" => 12 // Id from the db where to save or update response
], (*50)
$result = $hackerearth->Run([$data]);, (*51)
dd($result);, (*52)
OR, (*53)
Output::saveResult(json_decode($result, True)); // Save directly to the DB, (*54)
// Response at Callback URL will save to DB with reference to the ID, (*55)
Output::savePayload(json_decode($_POST['payload'], True));, (*56)
```, (*57)
## Also Compile and Run files by passing realpath of the uploaded file--, (*58)
```php
use Ankitjain28may\HackerEarth\Facades\HackerEarth;, (*59)
//..
//.., (*60)
$data = [
"lang" => 'php',
"source" => realpath("test.txt")
];, (*61)
$result = HackerEarth::RunFile([$data]);
$result = HackerEarth::CompileFile([$data]);, (*62)
```, (*63)
## Contribute, (*64)
Feel free to contribute, (*65)
License
Copyright (c) 2017 Ankit Jain - Released under the MIT License, (*66)