
Teach you to use PHP to play with proxy IP handily
Brothers engaged in network development understand that sometimes it is easy to send requests directly from their own IP to eat the door. At this time you need a proxy IP to save the day, today we take PHP to open the knife, demonstrating a few practical cases of grounded.
What can a proxy IP really do?
In a nutshell.Send the request in a different armor.. For example:
- When collecting data is restricted by the website
- When it is necessary to simulate users in different regions
- When changing your identity for a batch registration account
- When testing the site's geo-restricted functionality
Three poses for PHP to mess with proxies
Here we recommend ipipgo's proxy service, the stability of the pro-test is good. Their agents are divided into three types, let's use the table to compare more intuitive:
| typology | tempo | anonymity | Scenario |
|---|---|---|---|
| Transparent Agent | plain-spoken | lower (one's head) | General Data Acquisition |
| Anonymous agent | moderate | center | Routine operational requirements |
| High Stash Agents | slightly slower | your (honorific) | Sensitive business operations |
Practical code to go
First the whole basic curl example, remember to fill in the proxy address given to you by ipipgo:
$proxy = '123.123.123.123:8888'; //proxy address provided by ipipgo
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://目标网站.com');
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
if it needsAutomatic switching of proxy poolsIt can be screwed up like this:
// Array of proxy pools obtained from ipipgo
$proxyList = [
'111.222.33.44:8080',
'55.66.77.88:3128',
'99.100.101.102:8888'
];
foreach ($proxyList as $proxy) {
foreach ($proxyList as $proxy) { try {
//... Put the curl code from above here...
if($result) break; //Break out of the loop if the request is successful.
} catch (Exception $e) {
//Record the failure log
}
}
Frequently Asked Questions QA
Q: What should I do if my proxy IP often fails?
A: It is recommended to use ipipgo's dynamic proxy service, they automatically change a batch of IPs every 5 minutes, which is much more worrying than their own maintenance.
Q: Can I use a proxy for HTTPS requests?
A: Required! Add these two sentences to the curl settings:
CURLOPT_PROXYTYPE = CURLPROXY_HTTP
CURLOPT_HTTPPROXYTUNNEL = true
Q: How can I tell if a proxy is in effect?
A: Add acurl_getinfo($ch, CURLINFO_PRIMARY_IP)The IP printed out is not the local one, that's right.
Guide to avoiding the pit
1. Don't try to use a free proxy, nine out of ten are pits. Before using ipipgo's paid proxy, the success rate can be more than 98%.
2. Remember to set a timeout, recommendCURLOPT_TIMEOUTNo more than 10 seconds.
3. Important operations should be paired with a retry mechanism, with three consecutive failures before switching agents.
One last tip: when using ipipgo's API interface to get a proxy, add the?format=phpParameters can be directly to the PHP array format, to save the trouble of parsing. Specific usage in their home development documentation has a detailed description, here will not nag.

