Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
numairawan committed Sep 29, 2023
0 parents commit 933fd0c
Show file tree
Hide file tree
Showing 7 changed files with 376 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Numair Awan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<p align="center">
<img src="https://raw.githubusercontent.com/NumairAwan/ethplorer-api-php/main/art/ethplorer-api-php.png" width="600" alt="ethplorer-api-php">
<p align="center">
<a href="https://packagist.org/packages/numairawan/ethplorer-api-php"><img alt="Total Downloads" src="https://img.shields.io/packagist/dt/numairawan/ethplorer-api-php"></a>
<a href="https://packagist.org/packages/numairawan/ethplorer-api-php"><img alt="Latest Version" src="https://img.shields.io/packagist/v/numairawan/ethplorer-api-php"></a>
<a href="https://packagist.org/packages/numairawan/ethplorer-api-php"><img alt="License" src="https://img.shields.io/github/license/numairawan/ethplorer-api-php"></a>
</p>
</p>

------

# Ethplorer API PHP Library

🚀 This PHP library allows you to interact with the Ethplorer.io API. It's designed for ease of use and offers the following features:

- 🔄 Support for multiple API keys to bypass rate limits on Ethplorer's free account API.
- 📦 Requires no external dependencies.
- ⚡ Delivers high-speed performance.
- 🔑 Offers two multi-key modes: sequential and random.

Get started quickly with Ethplorer API integration using this library.

## Installation

To install the library, you can use [Composer](https://getcomposer.org/) and run the following command:

```bash
composer require numairawan/ethplorer-api-php
```


### Usage
To interact with ethplorer.io, follow these simple steps:

```php
use NumairAwan\EthplorerApi\EthplorerApi;

// Example usage:
$apiKeys = ["YOUR_API_KEY_1", "YOUR_API_KEY_2"];

// By default it will use sequential mode with multi-keys for each request
// pass 'true' in 2nd parameter to use random api for each request.

// new EthplorerApi($apiKeys, true);
$ethplorer = new EthplorerApi($apiKeys);

// Fetch last block data
$lastBlockData = $ethplorer->getLastBlock();
var_dump($lastBlockData);
```

### Contributing
Contributions are welcome! Feel free to fork the repository and submit pull requests as well.

### License
This project is licensed under the **[MIT license](https://opensource.org/licenses/MIT)**.


## Connect with Me

Feel free to reach out to me for any project-related queries or collaborations. I'm always happy to connect and discuss ideas!

[<img align="left" alt="Telegram" width="32px" src="https://upload.wikimedia.org/wikipedia/commons/8/82/Telegram_logo.svg" />](https://t.me/NumairAwan)
[<img align="left" alt="WhatsApp" width="32px" src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/WhatsApp.svg/512px-WhatsApp.svg.png?20220228223904" />](https://wa.me/+923164700904)

Binary file added art/ethplorer-api-php.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "numairawan/ethplorer-api-php",
"description": "PHP library for Ethplorer.io API. Supports multiple keys for rate limits, no dependencies, and offers fast performance.",
"type": "library",
"keywords": ["ethereum", "ethereum-api", "ethereum-chain-api", "ethplorer", "ethplorer-api", "php-ethereum-api", "ethplorer-php-api"],
"license": "MIT",
"authors": [
{
"name": "Numair Awan",
"email": "numairawan1@gmail.com",
"homepage": "https://github.com/NumairAwan"
}
],
"require": {},
"autoload": {
"psr-4": {
"NumairAwan\\EthplorerApi\\": "src/"
}
},
"minimum-stability": "stable",
"prefer-stable": true
}
227 changes: 227 additions & 0 deletions src/EthplorerApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
<?php

namespace NumairAwan\EthplorerApi;

/**
* Ethplorer API class for interacting with Ethplorer.io
*/
class EthplorerApi
{

private $apiAddress = "https://api.ethplorer.io/";
private $apiKeys = [];
private $randKeys = false;
private $apiKeyIndex = 0;

/**
* Constructor to initialize the API with an array of API keys
*/
public function __construct(array $apiKeys, bool $randKeys)
{
$this->apiKeys = $apiKeys;
$this->randKeys = $randKeys;
}

/**
* Performs a cURL request to the given JSON URL
*
* @param string $URL The URL to fetch JSON data from
* @return mixed Decoded JSON response
*/
private function curl($URL)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_USERAGENT => 'Googlebot/2.1 (+http://www.google.com/bot.html)',
CURLOPT_HTTPHEADER => [
'Accept: application/json',
'Cache-Control: max-age=0',
'Connection: keep-alive',
'Keep-Alive: 300',
'Accept-Charset: UTF-8;q=0.7,*;q=0.7',
'Accept-Language: en-US,en;q=0.5',
'Pragma:'
],
CURLOPT_ENCODING => 'gzip,deflate',
CURLOPT_AUTOREFERER => true,
CURLOPT_VERBOSE => 0,
]);

$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
}

/**
* Get a random API key from the provided array of keys
*
* @return string Random API key
*/
private function getRandApi()
{
if (count((array) $this->apiKeys) === 0) {
return '';
}

if ($this->randKeys) {
return $this->apiKeys[array_rand($this->apiKeys)];
}

$index = $this->apiKeyIndex % count($this->apiKeys);
$this->apiKeyIndex++;
return $this->apiKeys[$index];
}

/**
* Get the last block information.
*
* @return array Last block information
*/
public function getLastBlock()
{
$url = "{$this->apiAddress}getLastBlock?apiKey={$this->getRandApi()}";
return $this->curl($url);
}

/**
* Get token information by token address.
*
* @param string $token The token address
* @return array Token information
*/
public function getTokenInfo($token)
{
$url = "{$this->apiAddress}getTokenInfo/{$token}?apiKey={$this->getRandApi()}";
return $this->curl($url);
}

/**
* Get address information by address.
*
* @param string $address The Ethereum address
* @return array Address information
*/
public function getAddressInfo($address)
{
$url = "{$this->apiAddress}getAddressInfo/{$address}?apiKey={$this->getRandApi()}";
return $this->curl($url);
}

/**
* Get transaction information by transaction hash.
*
* @param string $tx The transaction hash
* @return array Transaction information
*/
public function getTxInfo($tx)
{
$url = "{$this->apiAddress}getTxInfo/{$tx}?apiKey={$this->getRandApi()}";
return $this->curl($url);
}

/**
* Get token history by token address, type, and limit.
*
* @param string $token The token address
* @param string $type (Optional) Type of history (default: "transfer")
* @param int $limit (Optional) Limit the number of results (default: 10)
* @return array Token history
*/
public function getTokenHistory($token, $type = "transfer", $limit = 10)
{
$url = "{$this->apiAddress}getTokenHistory/{$token}?apiKey={$this->getRandApi()}&type={$type}&limit={$limit}";
return $this->curl($url);
}

/**
* Get address history by token address, type, and limit.
*
* @param string $token The token address
* @param string $type (Optional) Type of history (default: "transfer")
* @param int $limit (Optional) Limit the number of results (default: 10)
* @return array Address history
*/
public function getAddressHistory($token, $type = "transfer", $limit = 10)
{
$url = "{$this->apiAddress}getAddressHistory/{$token}?apiKey={$this->getRandApi()}&type={$type}&limit={$limit}";
return $this->curl($url);
}

/**
* Get address transactions by token address and limit.
*
* @param string $token The token address
* @param int $limit (Optional) Limit the number of results (default: 10)
* @return array Address transactions
*/
public function getAddressTransactions($token, $limit = 10)
{
$url = "{$this->apiAddress}getAddressTransactions/{$token}?apiKey={$this->getRandApi()}&limit={$limit}";
return $this->curl($url);
}

/**
* Get top tokens based on criteria and limit.
*
* @param string $criteria (Optional) Criteria for sorting (default: "cap")
* @param int $limit (Optional) Limit the number of results (default: 50)
* @return array Top tokens
*/
public function getTop($criteria = "cap", $limit = 50)
{
$url = "{$this->apiAddress}getTop?apiKey={$this->getRandApi()}&criteria={$criteria}&limit={$limit}";
return $this->curl($url);
}

/**
* Get top tokens.
*
* @return array Top tokens
*/
public function getTopTokens()
{
$url = "{$this->apiAddress}getTopTokens/?apiKey={$this->getRandApi()}";
return $this->curl($url);
}

/**
* Get top token holders by token address and limit.
*
* @param string $token The token address
* @param int $limit (Optional) Limit the number of results (default: 100)
* @return array Top token holders
*/
public function getTopTokenHolders($token, $limit = 100)
{
$url = "{$this->apiAddress}getTopTokenHolders/{$token}?apiKey={$this->getRandApi()}&limit={$limit}";
return $this->curl($url);
}

/**
* Get token history grouped by token address.
*
* @param string $token The token address
* @return array Token history grouped
*/
public function getTokenHistoryGrouped($token)
{
$url = "{$this->apiAddress}getTokenHistoryGrouped/{$token}?apiKey={$this->getRandApi()}";
return $this->curl($url);
}

/**
* Get token price history grouped by token address and period.
*
* @param string $token The token address
* @param int $period (Optional) Price history period in days (default: 365)
* @return array Token price history grouped
*/
public function getTokenPriceHistoryGrouped($token, $period = 365)
{
$url = "{$this->apiAddress}getTokenPriceHistoryGrouped/{$token}?apiKey={$this->getRandApi()}&period={$period}";
return $this->curl($url);
}
}
39 changes: 39 additions & 0 deletions tests/EthplorerApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace NumairAwan\EthplorerApi\Tests;

use PHPUnit\Framework\TestCase;
use NumairAwan\EthplorerApi\EthplorerApi;

class EthplorerApiTest extends TestCase
{
private $ethplorer;

protected function setUp(): void
{
// Replace with your actual API keys
$apiKeys = ["YOUR_API_KEY_1", "YOUR_API_KEY_2"];
$this->ethplorer = new EthplorerApi($apiKeys);
}

public function testGetLastBlock()
{
$result = $this->ethplorer->getLastBlock();
$this->assertIsArray($result);
}

public function testGetTokenInfo()
{
$token = "0xYourTokenAddress";
$result = $this->ethplorer->getTokenInfo($token);
$this->assertIsArray($result);
}

public function testGetTokenPriceHistoryGrouped()
{
$token = "0xYourTokenAddress";
$result = $this->ethplorer->getTokenPriceHistoryGrouped($token);
$this->assertIsArray($result);
}
}

0 comments on commit 933fd0c

Please sign in to comment.