PHP code example of danielgnh / polymarket-laravel
1. Go to this page and download the library: Download danielgnh/polymarket-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
danielgnh / polymarket-laravel example snippets
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');
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'));
}
}
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);
use PolymarketPhp\PolymarketLaravel\Facades\Polymarket;
// Access the Bridge API
$bridge = Polymarket::bridge();
// Supported chains: Ethereum, Arbitrum, Base, Optimism, Solana, Bitcoin
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;
}
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);
}
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...
}