Customize Control Costs with ScraperAPI Parameter in PHP

Learn how to set cost limits for each scrape using ScraperAPI’s parameters in PHP. Prevent overspending on premium requests in both sync and async modes.

ScraperAPI helps you control and manage your costs efficiently. By using the max_cost parameter with your requests, you instruct the API to set a limit on the maximum API credits you'd like to spend per each individual scrape. This helps prevent overspending, ensuring you stay within your individual project's budget.

  • API REQUEST

<?php
$url =
"https://api.scraperapi.com?api_key=API_KEY&premium=true&max_cost=5&url=https://example.com"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,
TRUE); curl_setopt($ch, CURLOPT_HEADER,
FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,
0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,
0); $response = curl_exec($ch); curl_close($ch); print_r($response);
  • ASYNC REQUEST

<?php
$payload = json_encode(
    array(
        "apiKey" => "API_KEY",
        "url" => "https://example.com",
        "apiParams" => array(
            "premium" => "true",
            "max_cost" => 5
        )
    )
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://async.scraperapi.com/jobs");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$response = curl_exec($ch);
curl_close($ch);
print_r($response);
?>
  • PROXY MODE - COMING SOON!

If the scrape cost exceeds your limit, a 403 status code will be returned for the request, with the following error message:

"This request would cost more than your max_cost. You can see the cost per request in your response header or in API Playground in the dashboard."

Last updated

Was this helpful?