【问题标题】:How to mention ip port in random ip curl script如何在随机 ip curl 脚本中提及 ip 端口
【发布时间】:2017-08-27 07:38:38
【问题描述】:

我有一个用于 curl 的随机 IP 脚本,但我不知道如何将端口用于相同的随机脚本

function curl_get($kix) 
 {
 $ips = array(
     '85.10.230.132',
     '88.198.242.9',
     '88.198.242.10',
     '88.198.242.11',
     '88.198.242.12',
     '88.198.242.13',
     '88.198.242.14',
  );
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_URL, $kix);
  @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  curl_setopt($ch, CURLOPT_INTERFACE, $ips[rand(0, count($ips)-1)]);
  curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  $html = curl_exec($ch);
  curl_close($ch);
  return $html; 
 }

你可以在这里看到我已经提到了 ip,但我不知道如何为这些 ip 提及端口。

提前致谢!

【问题讨论】:

  • 是固定端口还是随机端口?
  • @azarudeen 对于每个IP,你有不同的端口吗?
  • 是的,每个 ip 我都有不同的端口
  • @funilrys 每个ip的固定端口

标签: php curl proxy


【解决方案1】:

如果每个 IP 有不同的端口,则可以使用关联数组将端口与 IP 相关联。

所以对于以下函数,我假设您使用 PHP >= 7.0 和以下作为我们数组的格式。

array('IP'=>'Port');

我们还 CURLOPT_PORT 分配备用 IP 进行连接。 引用documentation

CURLOPT_PORT:要连接的备用端口号。

回到你的问题:

  • 我使用curl_setopt($ch, CURLOPT_PORT, $port); 来分配我们需要使用的端口。
  • 我使用array_rand($ips)从新创建的关联数组(IP列表)中获取随机密钥
  • 最后我得到了它的端口,可以使用$port = $ips[$randomIP];直接访问阵列

所以你的函数现在看起来像:

function curl_get($kix)
{
    $ips = array(
         '85.10.230.132' => '80',
         '88.198.242.9' => '8080',
         '88.198.242.10' => '5754',
         '88.198.242.11' => '80',
         '88.198.242.12' => '8888',
         '88.198.242.13' => '8989',
         '88.198.242.14' => '8080',
      );

    // We get a random key (IP)
    $randomIP = array_rand($ips);

    // We get the port according to the random key (IP)
    $port = $ips[$randomIP];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $kix);
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_INTERFACE, $randomIP);

    // We set the alternative port with the following
    curl_setopt($ch, CURLOPT_PORT, $port);

    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

    $html = curl_exec($ch);
    curl_close($ch);

    return $html;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2020-09-22
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    相关资源
    最近更新 更多