php - 我怎样才能使 php 可以从 txt 文件加载代理,然后在使用 curlopt_proxy 时“旋转”它们
问题描述
这是我尝试过的一些..我仍然收到错误调用成员函数getProxy()和未定义变量:旋转,在我定义了getproxy函数之后,我想我是盲目的,因为有些东西我看不到,请如果你有一些建议,分享它:)。PS:我所说的旋转是通过随机化每个代理并在使用 curl “post”时使用它,如果这是一个糟糕的帖子,我很抱歉。
Class RotateProxy{
private $proxy = '';
private $fp = '';
private $list = array();
private $counter; //number of times proxy used. Default is 1
private $current; //number of times proxy HAS been used.
public function __constructor($path,$counter=1){
$this->list = trim(file($path));
$this->$counter = $counter;
}
public function getProxy(){
if($this->current > $this->counter){
array_shift($this->list);
$this->current = 1;
$this->proxy = $this->list[0];
return $this->proxy;
}else{
$this->current++;
return $this->proxy;
}
}
}
$proxylist = __DIR__ . 'proxies.txt'; // just an example.
$proxy = $rotate->getProxy();
$counter = 1; // proxy will be used 5 times before moving to the next proxy.
$rotate = new RotateProxy($proxylist,$counter);
$session = mt_rand();
$curl = curl_init('https://api.ipify.org/');
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_PROXY, $proxy);
$result = curl_exec($curl);
curl_close($curl);
if ($result)
echo $result;
?>```
解决方案
在调用实例上的方法之前,您必须创建 RotateProxy 类的实例。即这样:
$rotate = new RotateProxy($proxylist,$counter);
$proxy = $rotate->getProxy();
推荐阅读
- python - Pandas:如何仅删除列末尾的最后一个连字符
- google-forms - 如何根据其他用户过去的回复使 Google 表单自动填充?
- c - 我知道一个地址,但我应该如何判断在哪个部分?使用C的全局区域或堆区域或堆栈区域
- json - 如何解决“手势 RangeError(索引)捕获的异常:无效值:有效值范围为空:0”
- c++ - 什么会使多边形算法中的点仅在正向和反向运行时才能正常工作?
- php - PHP:如何使用键->值对每两个分隔符将一个字符串分解为一个数组
- kubernetes - 将 GKE 集群拆除为“全新”状态而不删除它?
- scala - Spark——自定义reduce函数保存到磁盘然后上传到s3
- javascript - XMLHttpRequest 返回事件对象而不是实际数据
- fortran - Fortran 将未知大小的文件读入数组