Callbacks

Using a status URL is a great way to test the API or get started quickly, but some customer environments may require some more robust solutions, so we implemented callbacks. Currently only webhook callbacks are supported but we are planning to introduce more over time (e.g. direct database callbacks, AWS S3, etc).

An example of using a webhook callback:

<?php
$payload = json_encode(
array(
"apiKey"
=>
"xxxxxx",
"callback"
=>
array(
"type"
=>
"webhook",
"url"
=>
"https://yourcompany.com/scraperapi"
),
"url"
=>
"https://example.com"
) ); $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
); $response = curl_exec( $ch ); curl_close( $ch ); print_r( $response );

Using a callback you don’t need to use the status URL (although you still can) to fetch the status and results of the job. Once the job is finished the provided webhook URL will be invoked by our system with the same content as the status URL provides.

Just replace the https://yourcompany.com/scraperapi URL with your preferred endpoint. You can even add basic auth to the URL in the following format: https://user:pass@yourcompany.com/scraperapi

Note: The system will try to invoke the webhook URL 3 times, then it cancels the job. So please make sure that the webhook URL is available through the public internet and will be capable of handling the traffic that you need.

Hint: Webhook.site is a free online service to test webhooks without requiring you to build a complex infrastructure.

Last updated