Run PHP scripts using rotating proxies
Sometimes, we want our PHP code to run behind the proxies to hide our IP address.
This tutorial will explain how to set up rotating proxies using a bash script.
Step1:
Create the PHP script which needs to be run behind the proxies.
eg. index.php
1 2 |
<?php $response = file_get_contents('https://example.com/'); |
Suppose we want to scrap the data fromexample.com and we don’t want to be banned from the example.com site server.
Step2:
Create the proxy.sh file on our server file system.
proxy.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#!/bin/bash #set the array of proxy ip's with the credentials and port information. #exclude the domains for which we don't want this proxy connection export NO_PROXY="localhost,127.0.0.1" changeProxy(){ IPS=( "${IPS[@]:1}" "${IPS[@]:0:1}" ) echo "Using this IP ${IPS[0]}"; export HTTP_PROXY=${IPS[0]} } while [ -n "$IPS" ]; do changeProxy; result=$( php ./index.php ) if [[ $result == *"20 attempts were failed"* ]]; then echo "${IPS[0]} Might be banned!" unset 'IPS[0]'; IPS=("${IPS[@]}") fi done |
make sure your index.php and proxy.sh are in the same folder.
make the proxy.sh file executable by firing this command: chmod u+x proxy.sh
Step3:
to test the functionality run the proxy.sh file using ./proxy.sh command.
now our PHP script will run behind the proxy server.
This tutorial will be helpful when you are scraping big data and don’t want to get banned.