Parameters as Headers | PHP
Learn how to scrape websites using header-based parameters with ScraperAPI in PHP. Pass api_key, render, and instruction_set via headers to simplify requests.
Along with the “traditional” means of passing parameters, we also support passing parameters as headers. Passing parameters such as api_key
, render
, ultra_premium
and instruction_set
is very straightforward.
API REQUEST
Instead of including the parameters in the URL
<?php $url =
"http://api.scraperapi.com?api_key=APIKEY&url=http://httpbin.org/ip&render=true"; $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);
you can just pass them as headers
<?php
$url = "https://api.scraperapi.com?url=https://httpbin.org/ip";
$ch = curl_init();
$headers = array(
'x-sapi-render: true',
'x-sapi-api_key: <YOUR_API_KEY>'
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
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);
?>
PROXY MODE
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://httpbin.org/ip");
curl_setopt($ch, CURLOPT_PROXY, "http://scraperapi:<YOUR_API_KEY>@proxy-server.scraperapi.com:8001");
$headers = array('x-sapi-render: true');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
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);
?>
Note that credentials must still be passed to the proxy in the manner shown above, not as headers.
Last updated
Was this helpful?