Skip to content

Instantly share code, notes, and snippets.

@rpi4gx
Created September 2, 2023 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpi4gx/462d15030efeadaa05a100fe3868f67a to your computer and use it in GitHub Desktop.
Save rpi4gx/462d15030efeadaa05a100fe3868f67a to your computer and use it in GitHub Desktop.
<?php
function obtain_proxy() {
$url = "https://ephemeral-proxies.p.rapidapi.com/v2/datacenter/proxy";
$headers = [
"X-RapidAPI-Key: " . getenv('RAPID_API_KEY'),
"X-RapidAPI-Host: ephemeral-proxies.p.rapidapi.com"
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if ($response === false) {
echo "Request failed with error: " . curl_error($ch);
return null;
}
curl_close($ch);
return json_decode($response, true);
}
function main() {
// Obtain a proxy from the API
$api_response = obtain_proxy();
if ($api_response !== null) {
print_r($api_response);
// Set up the proxy configuration
$proxy_host = $api_response['proxy']['host'];
$proxy_port = $api_response['proxy']['port'];
$proxy_url = "http://$proxy_host:$proxy_port";
$proxy_config = [
'http' => $proxy_url,
'https' => $proxy_url
];
// Visit a website using the proxy obtained
try {
$ch = curl_init('https://ifconfig.co/ip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, $proxy_url);
$response = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status_code == 200) {
echo "Website response:\n";
echo $response;
} else {
echo "Request failed with status code: $status_code\n";
}
} catch (Exception $e) {
echo "Request failed with error: " . $e->getMessage() . "\n";
}
}
}
if (getenv('RAPID_API_KEY') !== false) {
main();
} else {
echo "RAPID_API_KEY environment variable is not set.\n";
}
?>
@rpi4gx
Copy link
Author

rpi4gx commented Sep 2, 2023

To run it $ RAPID_API_KEY=_MY_KEY_HERE_ php ephemeral-proxies-sample.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment