PHP Code Examples
The following are code examples on how to integrate the ScrapeOps Residential Proxy Aggregator with your PHP based web scrapers using cURL library.
Authorisation - API Key
To use the ScrapeOps proxy, you first need an API key which you can get by signing up for a free account here.
Your API key must be included with every request using the api_key
query parameter otherwise the API will return a 403 Forbidden Access
status code.
Basic Request
The following is some example code to send a URL to the ScrapeOps Proxy port http://scrapeops:YOUR_API_KEY@residential-proxy.scrapeops.io:8181
:
<?php
$api_key = 'YOUR_API_KEY';
$url = 'https://httpbin.org/ip';
$proxy_url = 'http://scrapeops:' . $api_key . '@residential-proxy.scrapeops.io:8181';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_PROXY, $proxy_url);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
} else {
echo 'Body: ' . $response;
}
curl_close($ch);
?>
ScrapeOps will take care of the proxy selection and rotation for you so you just need to send us the URL you want to scrape.
If you want to send multiple requests and manage concurrency we suggest you use a HTTP client such as [Guzzle] (https://github.com/guzzle/guzzle). This is easier to use and helps you to use asynchronous requests in a simple manner.
Response Format
After receiving a response from one of our proxy providers the ScrapeOps Residential Proxy Aggregator will then respond with the raw HTML content of the target URL along with a response code:
<html>
<head>
...
</head>
<body>
...
</body>
</html>
Status Codes
The ScrapeOps Residential Proxy Aggregator will return the status code returned by the target website.
However, if the proxy port will return the following status codes if there are specific errors in your request:
Status Code | Billed | Description |
---|---|---|
400 | No | Bad request. Either your url or query parameters are incorrectly formatted. |
401 | No | You have consumed all your credits. Either turn off your scraper, or upgrade to a larger plan. |
403 | No | Either no api_key included on request, or api_key is invalid. |
Here is the full list of status codes the Proxy Port returns.
Advanced Functionality
To enable advanced proxy functionality when using the Residential Proxy endpoint you need to pass parameters by adding them to username, separated by periods.
For example, if you want to enable Country Geotargeting with a request, the username would be scrapeops.country=us
.
<?php
$api_key = 'YOUR_API_KEY';
$url = 'https://httpbin.org/ip';
$proxy_url = 'http://scrapeops.country=us:' . $api_key . '@residential-proxy.scrapeops.io:8181';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_PROXY, $proxy_url);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
} else {
echo 'Body: ' . $response;
}
curl_close($ch);
?>
Check out this guide to see the full list of advanced functionality available.
Timeout
The ScrapeOps proxy keeps retrying a request for up to 2 minutes before returning a failed response to you.
To use the Proxy correctly, you should set the timeout on your request to a least 2 minutes to avoid you getting charged for any bandwidth that you consumed before the Proxy responded.