polymarket-php/polymarket-laravel 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

polymarket-php/polymarket-laravel

最新稳定版本:v0.1.4

Composer 安装命令:

composer require polymarket-php/polymarket-laravel

包简介

Laravel adapter for the Polymarket PHP - Integrate prediction markets into your Laravel applications

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

This is a Laravel adapter for the Polymarket PHP SDK, providing easy ways to integrate polymarket in your Laravel application.

Requirements

  • PHP 8.2 or higher
  • Laravel 10.x, 11.x, or 12.x

Installation

You can install the package via Composer:

composer require polymarket-php/polymarket-laravel

Quick Setup

Run the install command for guided setup:

php artisan polymarket:install

This will:

  • Publish the configuration file
  • Display instructions for adding credentials to your .env file

Manual Setup

Alternatively, you can publish the configuration file manually:

php artisan vendor:publish --tag="polymarket-config"

Configuration

Add your Polymarket credentials to your .env file:

POLYMARKET_API_KEY=your-api-key
POLYMARKET_PRIVATE_KEY=0x...
POLYMARKET_CHAIN_ID=137

Configuration Options

All configuration options can be customized in config/polymarket.php:

Option Environment Variable Default Description
api_key POLYMARKET_API_KEY null API key for authenticated requests (optional for read-only)
private_key POLYMARKET_PRIVATE_KEY null Private key for trading operations
chain_id POLYMARKET_CHAIN_ID 137 Blockchain network (137 = Polygon mainnet)
gamma_base_url POLYMARKET_GAMMA_BASE_URL https://gamma-api.polymarket.com Gamma API base URL
clob_base_url POLYMARKET_CLOB_BASE_URL https://clob.polymarket.com CLOB API base URL
bridge_base_url POLYMARKET_BRIDGE_BASE_URL https://bridge-api.polymarket.com Bridge API base URL
timeout POLYMARKET_TIMEOUT 30 Request timeout in seconds
retries POLYMARKET_RETRIES 3 Number of retry attempts
verify_ssl POLYMARKET_VERIFY_SSL true Enable SSL certificate verification

Usage

Using the Facade

The facade provides convenient static access to the Polymarket client:

use PolymarketPhp\PolymarketLaravel\Facades\Polymarket;

// Fetch markets
$markets = Polymarket::gamma()->markets()->all();

// Search for specific markets
$markets = Polymarket::gamma()->markets()->search('election');

// Get a specific market
$market = Polymarket::gamma()->markets()->get('market-id');

Dependency Injection

Inject the client directly into your classes:

use PolymarketPhp\Polymarket\Client;

class MarketController extends Controller
{
    public function __construct(
        private Client $polymarket
    ) {}

    public function index()
    {
        $markets = $this->polymarket->gamma()->markets()->all();

        return view('markets.index', compact('markets'));
    }

    public function show(string $id)
    {
        $market = $this->polymarket->gamma()->markets()->get($id);

        return view('markets.show', compact('market'));
    }
}

Trading Operations (CLOB API)

For trading operations, you need to configure both an API key and private key:

use PolymarketPhp\PolymarketLaravel\Facades\Polymarket;

// Authenticate for trading
Polymarket::auth();

// Place an order
$order = Polymarket::clob()->orders()->create([
    'market' => 'market-id',
    'side' => 'BUY',
    'price' => 0.5,
    'size' => 100,
]);

// Get your orders
$orders = Polymarket::clob()->orders()->list();

// Cancel an order
Polymarket::clob()->orders()->cancel($orderId);

Bridge API (Cross-Chain Deposits)

The Bridge API enables deposits from multiple chains, automatically converting to USDC.e on Polygon:

use PolymarketPhp\PolymarketLaravel\Facades\Polymarket;

// Access the Bridge API
$bridge = Polymarket::bridge();

// Supported chains: Ethereum, Arbitrum, Base, Optimism, Solana, Bitcoin

Market Data Examples

Fetching All Markets

use PolymarketPhp\PolymarketLaravel\Facades\Polymarket;

// Get all markets with pagination
$markets = Polymarket::gamma()->markets()->all([
    'limit' => 20,
    'offset' => 0,
]);

foreach ($markets as $market) {
    echo $market['question'] . PHP_EOL;
}

Searching Markets

// Search for election-related markets
$results = Polymarket::gamma()->markets()->search('election', [
    'limit' => 10,
]);

// Search with filters
$results = Polymarket::gamma()->markets()->search('crypto', [
    'active' => true,
    'closed' => false,
]);

Getting Market Details

$marketId = 'some-market-id';
$market = Polymarket::gamma()->markets()->get($marketId);

echo "Question: {$market['question']}" . PHP_EOL;
echo "Volume: {$market['volume']}" . PHP_EOL;
echo "Liquidity: {$market['liquidity']}" . PHP_EOL;

Error Handling

The SDK throws specific exceptions for different error scenarios:

use PolymarketPhp\Polymarket\Exceptions\AuthenticationException;
use PolymarketPhp\Polymarket\Exceptions\NotFoundException;
use PolymarketPhp\Polymarket\Exceptions\RateLimitException;
use PolymarketPhp\Polymarket\Exceptions\ValidationException;
use PolymarketPhp\PolymarketLaravel\Facades\Polymarket;

try {
    $market = Polymarket::gamma()->markets()->get($marketId);
} catch (NotFoundException $e) {
    // Market not found
    return response()->json(['error' => 'Market not found'], 404);
} catch (RateLimitException $e) {
    // Rate limit exceeded
    return response()->json(['error' => 'Too many requests'], 429);
} catch (AuthenticationException $e) {
    // Authentication failed
    return response()->json(['error' => 'Unauthorized'], 401);
} catch (ValidationException $e) {
    // Invalid request parameters
    return response()->json(['error' => $e->getMessage()], 422);
}

Testing

The package includes comprehensive tests:

# Run tests
composer test

# Run static analysis
composer analyse

# Fix code style
composer format

Mocking in Your Tests

You can easily mock the Polymarket client in your tests:

use PolymarketPhp\PolymarketLaravel\Facades\Polymarket;
use PolymarketPhp\Polymarket\Client;

public function test_it_fetches_markets()
{
    $mockClient = Mockery::mock(Client::class);
    $mockClient->shouldReceive('gamma->markets->all')
        ->once()
        ->andReturn(['market1', 'market2']);

    $this->app->instance(Client::class, $mockClient);

    // Your test code...
}

Advanced Configuration

Custom Timeouts

For long-running operations, adjust the timeout:

POLYMARKET_TIMEOUT=60

Retry Configuration

Configure automatic retry behavior:

POLYMARKET_RETRIES=5

Local Development

Disable SSL verification for local testing (not recommended for production):

POLYMARKET_VERIFY_SSL=false

Custom API URLs

Use custom API endpoints (useful for testing):

POLYMARKET_GAMMA_BASE_URL=https://custom-gamma-api.example.com
POLYMARKET_CLOB_BASE_URL=https://custom-clob-api.example.com

Changelog

Please see CHANGELOG for more information on recent changes.

Contributing

Contributions are welcome! In case if you found any bug or have an idea just open an issue and create a Pull Request.

Security

If you discover any security-related issues, please email security@example.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 43
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固