curl 更换ip访问

public function curl_string ($url){
                $user_agent = "Mozilla/4.0";
                $proxy = "http://".$this->get_ip();
               $ch = curl_init();
               curl_setopt ($ch, CURLOPT_PROXY, $proxy);
               curl_setopt ($ch, CURLOPT_URL, $url);
               curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
              // curl_setopt ($ch, CURLOPT_COOKIEJAR, "./cookie.txt");
               curl_setopt ($ch, CURLOPT_HEADER, 1);
               curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
               curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);

               curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
               $result = curl_exec ($ch);
               curl_close($ch);
               return $result;
        }
public function get_ip(){
        $url="www.baidu.com"; //获取ip地址的ip池
        $ip_json=file_get_contents($url);
        $ip_arr=json_decode($ip_json,true);
/*        $ip['ip']=$ip_arr[0]['http_ip'];
        $ip['port']=$ip_arr[0]['http_port'];*/
        $ip_port=$ip_arr[0]['http_ip'].':'.$ip_arr[0]['http_port'];
        return $ip_port;
        //return $ip;
    }

 

posted @ 2018-09-26 15:15  zrn  阅读(1276)  评论(0编辑  收藏  举报