简体   繁体   English

如何让 php 脚本使用代理列表

[英]how to make php script use a list of proxies

Im using the Google pagerank checking script found here:我正在使用此处找到的 Google pagerank 检查脚本:

http://www.off-soft.net/en/develop/php/prcheck.html http://www.off-soft.net/en/develop/php/prcheck.html

I've noticed however that after too many requests the server gets a temporary ban.但是我注意到,在请求过多之后,服务器会被临时禁止。

I'd like to somehow route requests through a list of proxy servers - can anyone get me started?我想以某种方式通过代理服务器列表路由请求 - 任何人都可以让我开始吗?

I'm looking for any code examples of php requests using a list of proxies.我正在寻找使用代理列表的 php 请求的任何代码示例。

Thanks!!谢谢!!

PHP's curl library allows you to use socks5 and http proxies. PHP 的 curl库允许您使用 socks5 和 http 代理。 A list of proxy servers should be verified using a tool like YAPH before using them.代理服务器列表应在使用前使用YAPH 等工具进行验证。

The temporary ban is to prevent abuse.临时禁令是为了防止滥用。 Using proxies to bypass the ban isn't exactly a nice thing to do.使用代理绕过禁令并不是一件好事。 So, you're not likely to find anyone here who'll help you violate that site's TOS.因此,您不太可能在这里找到可以帮助您违反该网站 TOS 的人。

That being said, a proxy for HTTP is just a webserver that'll process/honor requests for foreign/outside URLs and return the results.话虽如此,HTTP 代理只是一个网络服务器,它将处理/尊重对外部/外部 URL 的请求并返回结果。 The rest is left as an exercise to the asker.其余的留给提问者作为练习。

If you don't mind using paid API, then gimmeproxy.com will work for you.如果您不介意使用付费 API,那么 gimmeproxy.com 将为您服务。

<?php
function getProxy() {
    $data = json_decode(file_get_contents('http://gimmeproxy.com/api/getProxy?api_key=YOUR_API_KEY'), 1);
    if(isset($data['error'])) { // there are no proxies left for this user-id and timeout
        echo $data['error']."\n";
    } 
    return isset($data['error']) ? false : $data['curl']; //gimmeproxy returns 'curl' field that is CURLOPT_PROXY-ready string, see curl_setopt($curl, CURLOPT_PROXY, $proxy);
}

function get($url) {
    $curlOptions = array(
        CURLOPT_CONNECTTIMEOUT => 5, // connection timeout, seconds
        CURLOPT_TIMEOUT => 10, // total time allowed for request, second
        CURLOPT_URL => $url,
        CURLOPT_SSL_VERIFYPEER => false, // don't verify ssl certificates, allows https scraping
        CURLOPT_SSL_VERIFYHOST => false, // don't verify ssl host, allows https scraping
        CURLOPT_FOLLOWLOCATION => true, // follow redirects
        CURLOPT_MAXREDIRS => 9, // max number of redirects
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_HEADER => 0,
        CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
        CURLINFO_HEADER_OUT  => true,
    );
    $curl = curl_init();
    curl_setopt_array($curl, $curlOptions);
    if($proxy = getProxy()) {
        echo 'set proxy '.$proxy."\n";
        curl_setopt($curl, CURLOPT_PROXY, $proxy);
    }
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}
while(true) {
    $data = get('https://news.ycombinator.com/');
    if(trim($data) && stripos($data, 'Hacker News') !== false) {
        echo "hacker news works fine";
        break;
    } else {
        echo "hacker news banned us, try another proxy\n";
    }
}

Sample PHP CURL request using a Squid Proxy:使用 Squid 代理的示例 PHP CURL 请求:

$proxy = "1.1.1.1:12121";
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$url = "http://www.google.pt/search?q=anonymous";

$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,15);
curl_setopt($ch, CURLOPT_HTTP_VERSION,'CURL_HTTP_VERSION_1_1' );
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD,'USER:PASS');
curl_setopt($ch, CURLOPT_USERAGENT,$useragent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$result = curl_exec ($ch);
curl_close ($ch);

Learn how to implement your own squid proxy with rotating outgoing ip's here 在此处了解如何使用旋转传出 ip 实现您自己的鱿鱼代理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM