
Hands-on with PHP to build a proxy relay station
Recently, many friends asked the old Zhang, said they want to proxy server transit data, but do not want to engage in too complex a system. Today we will nag with PHP to quickly build HTTP proxy method, it is guaranteed to be more than those old online tutorials really. By the way, remember to use a professional proxy IP service, like theipipgoThis reliable supplier will be detailed later on how to work with it.
Why do you need to do your own proxy transit?
For example, you opened an e-commerce site to catch the price of competing products, the direct crawl is easy to be blocked. At this time to engage in a transit agent, the request is dispersed to different IP, as if the crawler wears a myriad of masks. The advantages of using PHP to do this areFast deployment,Easy to change the code, especially suitable for scenarios that require frequent rule adjustments.
Get ready for your stuff.
Make sure you have a server that can run PHP first, don't use those free spaces, the IP quality is too bad. RecommendedDedicated IP servers for ipipgo, their home IP pool is clean and not easily recognized by target websites. Specific needs:
| matrix | request |
|---|---|
| PHP version | ≥ 7.3 (don't use the old 5.x) |
| expansion module | Curl, openssl must be installed. |
| server network | At least 5M bandwidth |
The core code is written like this
Create a new proxy.php file, the core is just these dozen lines of code:
<?php $target = $_GET['url']; //要转发的地址 $proxy_ip = 'ipipgo分配的代理IP'; //这里填从ipipgo后台拿的IP $port = 8899; //对应端口 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $target); curl_setopt($ch, CURLOPT_PROXY, $proxy_ip.":".$port); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //重点!加上ipipgo提供的认证信息 curl_setopt($ch, CURLOPT_PROXYUSERPWD, "用户名:密码"); $result = curl_exec($ch); curl_close($ch); echo $result; ?>
Be careful to putUser name and passwordReplace it with the authentication information you get in the backend of ipipgo, and all their proxies arevolumetric billingDon't be afraid to waste it.
How do I use it safely?
Don't be stupid and expose this script directly, do these three steps at a minimum:
1. Add aLocation InterceptionThe following is an example of a path that can be accessed only by a specific path.
2. Regularly go to the ipipgo back officeIP replacementRecommended to be changed every 100,000 requests
3. PlusAccess logI've got a record of who's using your proxy.
Common pitfalls QA
Q: What should I do if I return a 502 error?
A: 80% is the proxy IP failure, go to ipipgo background to check the IP status. They have aIP Health Checkfunction that automatically filters failed nodes.
Q: Access is slow as a snail?
A: Check the server geolocation! If using ipipgo'sDomestic BGP lineThe website can be accessed within 200ms.
Q: How do I prevent people from white-knighting?
A: Add a$_SERVER['REMOTE_ADDR']judgment to allow only specified IP calls. Or use ipipgo'sAPI forensicsFunctionality, double insurance.
Say something from the heart.
Self-built proxy is afraid that the IP quality is not good, before the use of free IP was pitched, three days to change the configuration. Later, I switched to usingipipgo's professional agency services, found that it does save money. One of their specialties isdynamic port mappingThe IP address of the server can be used as ten IP addresses, which is especially suitable for situations that require a large number of concurrencies.
As a final reminder, test first withpay-per-use packageIf you want to buy a monthly subscription, you need to be sure of your needs before you buy a monthly subscription. Don't be like me when I was stupid directly buy annual payment, the result of business adjustment waste of money. If you don't understand anything, go directly to the official website of ipipgo and look for online customer service, the reply speed is much faster than some big companies.

