-
-
Save rpi4gx/462d15030efeadaa05a100fe3868f67a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run it
$ RAPID_API_KEY=_MY_KEY_HERE_ php ephemeral-proxies-sample.php