1. curl代理ip

1.1. CURL 获取数据


    public function getDatas($url='http://www.baidu.com',$proxy='',$proxyport)
    {
        $CurlOptions = array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 10,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => array(
                "cache-control: no-cache",
            ),
        );
        if($proxy){
            $CurlOptions[CURLOPT_PROXY] = $proxy;
            $CurlOptions[CURLOPT_PROXYPORT] = $proxyport;
        }
            $curl = curl_init();
            curl_setopt_array($curl, $CurlOptions);
            $response = curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);
            if ($err) {
                return [false,$err];
            } else {
                return [true,$response];
            }
    }

1.2. 获取代理ip


    public function getIps()
    {
        $urlLists = [
          'http://www.xicidaili.com/wt/',
//          'http://www.xicidaili.com/wn/', //https
          'http://www.xicidaili.com/nt/',
          'http://www.xicidaili.com/nn/',
        ];
        $ipList = [];
        foreach($urlLists as $uval){
            $dataSource = file_get_contents($uval);
            $dataSource = preg_replace("/\\r\\n/", '', $dataSource);
            $dataSource = preg_replace("/\\r/", '', $dataSource);
            $dataSource = preg_replace("/\\n/", '', $dataSource);
            $dataSource = preg_replace("/\\t/", '', $dataSource);
            $dataSource = str_replace(" ", '', $dataSource);

            $reg = "/<trclass=[\"\w]+[^>].*?<td>([\d\.]+)<\/td><td>([\d]+)<\/td>/is";
            preg_match_all($reg,$dataSource,$matchs);

            if(isset($matchs[1])&&isset($matchs[2])){
                foreach($matchs[1] as $key=>$val){
                    $item['ip'] = $val;
                    $item['port'] = $matchs[2][$key];
                    array_push($ipList,$item);
                }
            }
        }
        return $ipList;
    }

results matching ""

    No results matching ""