curl 更换ip访问

本文展示了一个使用PHP的cURL库进行代理请求的代码示例。通过设置代理服务器、User-Agent、超时时间等参数,该函数可以发送HTTP请求并返回响应结果。此外,还提供了一个用于获取代理IP的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }

 

转载于:https://www.cnblogs.com/zrn-php/p/9707128.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值