Created
August 4, 2025 10:30
-
-
Save mezhevikin/d1dc8f9e31e5f2430d7be1c9b9ce4dca to your computer and use it in GitHub Desktop.
Lightweight Laravel service for interacting with Bybit’s P2P API. Supports HMAC SHA256 request signing and provides a getAds() method to fetch P2P listings. Uses Laravel’s Http facade and pulls API credentials from config/services.php.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Services; | |
| use Illuminate\Support\Facades\Http; | |
| class ByBitClient | |
| { | |
| private string $baseUrl = 'https://api.bybit.com'; | |
| protected int $recvWindow = 5000; | |
| public function __construct( | |
| protected string $apiKey, | |
| protected string $apiSecret | |
| ) {} | |
| public static function default(): self | |
| { | |
| return new self( | |
| config('services.bybit.key'), | |
| config('services.bybit.secret') | |
| ); | |
| } | |
| protected function getTimestamp(): string | |
| { | |
| return (string) round(microtime(true) * 1000); | |
| } | |
| protected function getHeaders(string $paramStr): array | |
| { | |
| $timestamp = $this->getTimestamp(); | |
| $payload = [ | |
| $timestamp, | |
| $this->apiKey, | |
| $this->recvWindow, | |
| $paramStr | |
| ]; | |
| $sign = hash_hmac( | |
| 'sha256', | |
| implode('', $payload), | |
| $this->apiSecret | |
| ); | |
| return [ | |
| 'X-BAPI-API-KEY' => $this->apiKey, | |
| 'X-BAPI-TIMESTAMP' => $timestamp, | |
| 'X-BAPI-RECV-WINDOW' => $this->recvWindow, | |
| 'X-BAPI-SIGN' => $sign | |
| ]; | |
| } | |
| protected function api(string $path, array $params = []) | |
| { | |
| $body = json_encode($params); | |
| $headers = $this->getHeaders($body); | |
| $url = $this->baseUrl . $path; | |
| return Http::withHeaders($headers) | |
| ->withBody($body) | |
| ->post($url) | |
| ->json(); | |
| } | |
| public function getAds( | |
| string $currencyId, | |
| string $tokenId = 'USDT', | |
| int $side = 0, | |
| int $page = 1, | |
| int $size = 10 | |
| ) { | |
| return $this->api('/v5/p2p/item/online', [ | |
| 'tokenId' => $tokenId, | |
| 'currencyId' => $currencyId, | |
| 'side' => (string) $side, | |
| 'page' => (string) $page, | |
| 'size' => (string) $size | |
| ]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment