PHP code example of joshfinlayau / laravel-anthropic-web

1. Go to this page and download the library: Download joshfinlayau/laravel-anthropic-web 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/ */

    

joshfinlayau / laravel-anthropic-web example snippets


use JoshFinlayAU\LaravelAnthropicWeb\Facades\AnthropicWeb;

$response = AnthropicWeb::complete('What are the latest developments in AI?');
echo $response;

$result = AnthropicWeb::completeWithWebSearch(
    'What are the current interest rates in Australia?',
    searchOptions: ['max_uses' => 3]
);

$analysis = AnthropicWeb::completeWithWebFetch(
    'Analyze the content at https://example.com/article',
    fetchOptions: [
        'citations' => ['enabled' => true],
        'allowed_domains' => ['example.com']
    ]
);

$research = AnthropicWeb::completeWithWebTools(
    'Research the latest trends in renewable energy and analyze the top 3 articles you find',
    searchOptions: ['max_uses' => 5],
    fetchOptions: ['citations' => ['enabled' => true]]
);

// Simple JSON response
$data = AnthropicWeb::completeJson('Return information about Paris as JSON');

// JSON with schema validation
$schema = [
    'type' => 'object',
    'properties' => [
        'name' => ['type' => 'string'],
        'population' => ['type' => 'integer'],
        'country' => ['type' => 'string']
    ]
];

$cityData = AnthropicWeb::completeJson(
    'Return information about Tokyo',
    $schema
);

// JSON response with web search
$currentData = AnthropicWeb::completeJsonWithWebSearch(
    'Find current stock price for Apple and return as JSON',
    ['type' => 'object', 'properties' => ['symbol' => ['type' => 'string'], 'price' => ['type' => 'number']]]
);

$searchTool = AnthropicWeb::webSearchTool([
    'max_uses' => 5,
    'allowed_domains' => ['reuters.com', 'bbc.com'],
    'user_location' => AnthropicWeb::userLocation('Sydney', 'NSW', 'AU', 'Australia/Sydney')
]);

$request = AnthropicWeb::buildMessageRequest(
    'claude-sonnet-4-20250514',
    [['role' => 'user', 'content' => 'Latest news about climate change']],
    4000,
    [$searchTool]
);

$response = AnthropicWeb::createMessage($request);

$stream = AnthropicWeb::createStreamedMessage([
    'model' => 'claude-sonnet-4-20250514',
    'max_tokens' => 4000,
    'messages' => [['role' => 'user', 'content' => 'Tell me about quantum computing']],
    'tools' => [AnthropicWeb::webSearchTool()]
]);

foreach ($stream as $chunk) {
    echo $chunk['content'][0]['text'] ?? '';
}

return [
    'api_key' => env('ANTHROPIC_API_KEY'),
    'models' => [
        'default' => 'claude-sonnet-4-20250514',
        'fast' => 'claude-3-5-haiku-latest',
        'powerful' => 'claude-opus-4-1-20250805',
    ],
    'web_tools' => [
        'search' => ['max_uses' => 5],
        'fetch' => ['max_uses' => 10, 'citations_enabled' => true],
    ],
];

AnthropicWeb::webSearchTool([
    'max_uses' => 5,
    'allowed_domains' => ['example.com'],
    'blocked_domains' => ['spam.com'],
    'user_location' => [
        'city' => 'Brisbane',
        'region' => 'Queensland',
        'country' => 'AU',
        'timezone' => 'Australia/Brisbane'
    ]
])

AnthropicWeb::webFetchTool([
    'max_uses' => 10,
    'allowed_domains' => ['trusted-site.com'],
    'citations' => ['enabled' => true],
    'max_content_tokens' => 100000
])

try {
    $response = AnthropicWeb::complete('Your prompt here');
} catch (\Exception $e) {
    Log::error('Anthropic API failed: ' . $e->getMessage());
}
bash
php artisan vendor:publish --tag=anthropic-web-config