从代理IP - PHP发送cURL请求


Send cURL request from Proxy IP - PHP

每当我试图获取服务器IP时,谷歌大多数时候都会阻止我的服务器IP。就像我们做一个Bulk Google PR检查脚本,Google会在接近3000或5000个请求后屏蔽我们的IP。为了给用户最好的结果,我们需要通过代理发送cURL请求。不仅是谷歌,我们有时也需要它来通过代理发送请求。所以,如果有人知道,请告诉过程,否则闭上你的嘴问这个跑题。我如何从代理发送cURL请求,而不是从我的服务器IP?或者告诉我更好的方法来抓取谷歌数据?

如何合法使用cURL

使用选项curl_setopt($ch, CURLOPT_PROXY, $proxy);

 //scape.php
class Scraper {
public function scrape($target_url) {
    $this->target_url = $target_url;
    $ch = curl_init();
    $proxy = $this->_getProxy();
    $userAgent = $this->_getUserAgent();
    if ($proxy) {
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
     }
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_URL, $target_url);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $html = curl_exec($ch);
    curl_close($ch);
    if (!$html) {
        echo 'url/curl error';
        return false;
    }
    $this->html = $html;
    $this->_ParseData();
}

public function setProxy($proxy) {
    $this->proxy = $proxy;
}
private function _getProxy() {
    if (isset($this->proxy))
        return $this->proxy;
    else
        return false;
}
public function setUserAgent($agent) {
    $this->agent = $agent;
}
private function _getUserAgent() {
    if (isset($this->agent))
        return $this->agent;
    else
        return false;
}
 //Parsing data
private function _parseData() {
    $dom = new DOMDocument();
    @$dom->loadHTML($this->html);
    $xpath = new DOMXPath($dom);
    // your xpath query here
    $elements = $xpath->query("//div[@id='ires']");
}
}

示例用法

require 'scrape.php';
$scraper=new Scraper;
$scraper->setProxy('127.0.0.1:9150');
$data=$scraper->scrape('https://www.google.com/#q=stack+overflow');