ankitjain28may/hackerearth-api

PHP和Laravel HackerEarth API包装器

V0.1 2017-07-08 15:12 UTC

This package is auto-updated.

Last update: 2024-09-19 21:52:16 UTC


README

Latest Stable Version Build Status Coverage Status Packagist

此包使用HackerEarth api进行编译和运行代码。

HackerEarth代码检查器API。一个非常简单的REST API。支持十多种语言。全部由可靠的HackerEarth服务器支持。你可以使用自己的评分系统或构建自己的在线评测系统。

PHP创新奖:Hacker Earth API的PHP包证书链接

安装

在项目目录中从终端运行此命令

composer require ankitjain28may/hackerearth-api

Laravel配置

下载完成后,您需要在config/app.php配置文件中调用此包服务。为此,请在app.php中的providers数组中添加此行

Ankitjain28may\HackerEarth\HackerEarthServiceProvider::class,

要使用外观,您需要在app.php中的aliases数组中添加此行

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

现在在终端中运行此命令以发布此包资源

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

发布配置文件后,打开config/hackerearth.php并添加您的HackerEarth应用密钥

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'),
];

您还可以在.env中添加API密钥

 HACKEREARTH_SECRET_KEY = YOUR_HACKER_EARTH_API_KEY

这就完成了。

API列表

    $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, ..])

异步请求

  • 设置async = 1
  • 您需要添加回调URL,输出将直接作为POST请求返回到回调URL。

同步请求

  • 设置async = 0
  • 输出将与请求的响应一起返回。

对于核心PHP使用

  • 创建数据库
create database [database name]
  • 导入表
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`.

对于Laravel使用

代码编译

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));

代码运行

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));

也可以通过传递上传文件的绝对路径来编译和运行文件--

use Ankitjain28may\HackerEarth\Facades\HackerEarth;

//..
//..

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

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

贡献

欢迎贡献

许可证

版权所有(c)2017 Ankit Jain - 在MIT许可证下发布