PHP Integration
BrowserCloud provides multiple ways to integrate with PHP applications, from simple REST API calls using cURL to comprehensive Laravel SDK integration. This guide covers everything from basic usage to advanced automation workflows.
Basic cURL Usage
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://chrome-v2.browsercloud.io/screenshot?token=YOUR_API_TOKEN_HERE",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
"url" => "https://example.com/",
"options" => [
"fullPage" => true,
"encoding" => "base64"
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
// Save the base64 encoded screenshot
file_put_contents('screenshot.png', base64_decode($response));
echo "Screenshot saved successfully!";
}
Using Guzzle HTTP Client (PHP 8.x)
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class BrowserCloudClient
{
private Client $client;
private string $apiToken;
private string $baseUrl;
public function __construct(string $apiToken, string $region = 'sfo')
{
$this->apiToken = $apiToken;
$this->baseUrl = "https://chrome-v2.browsercloud.io";
$this->client = new Client([
'timeout' => 60,
'headers' => [
'Content-Type' => 'application/json',
'User-Agent' => 'PHP-BrowserCloud-Client/1.0'
]
]);
}
public function screenshot(string $url, array $options = []): string
{
try {
$response = $this->client->post("{$this->baseUrl}/screenshot", [
'query' => ['token' => $this->apiToken],
'json' => [
'url' => $url,
'options' => array_merge([
'fullPage' => true,
'type' => 'png'
], $options)
]
]);
return $response->getBody()->getContents();
} catch (RequestException $e) {
throw new Exception("Screenshot failed: " . $e->getMessage());
}
}
public function generatePdf(string $url, array $options = []): string
{
try {
$response = $this->client->post("{$this->baseUrl}/pdf", [
'query' => ['token' => $this->apiToken],
'json' => [
'url' => $url,
'options' => array_merge([
'format' => 'A4',
'printBackground' => true
], $options)
]
]);
return $response->getBody()->getContents();
} catch (RequestException $e) {
throw new Exception("PDF generation failed: " . $e->getMessage());
}
}
public function getContent(string $url, int $waitFor = 0): array
{
try {
$response = $this->client->post("{$this->baseUrl}/content", [
'query' => ['token' => $this->apiToken],
'json' => [
'url' => $url,
'waitForTimeout' => $waitFor
]
]);
return json_decode($response->getBody()->getContents(), true);
} catch (RequestException $e) {
throw new Exception("Content retrieval failed: " . $e->getMessage());
}
}
}
Function Execution for Custom Logic
<?php
function executeCustomBrowserFunction(string $url, string $jsCode): array
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://chrome-v2.browsercloud.io/scenario?token=YOUR_API_TOKEN_HERE",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
"code" => $jsCode,
"context" => [
"url" => $url
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
}
// Example: Get page performance metrics
$performanceCode = '
export default async ({ page, context }) => {
await page.goto(context.url);
const metrics = await page.evaluate(() => {
const navigation = performance.getEntriesByType("navigation")[0];
return {
loadTime: navigation.loadEventEnd - navigation.loadEventStart,
domContentLoaded: navigation.domContentLoadedEventEnd - navigation.domContentLoadedEventStart,
firstPaint: performance.getEntriesByType("paint")[0]?.startTime || 0
};
});
return metrics;
};
';
$metrics = executeCustomBrowserFunction('https://example.com', $performanceCode);
echo "Page load time: " . $metrics['loadTime'] . "ms\n";