M
M
Miryl2018-11-13 13:08:26
PHP
Miryl, 2018-11-13 13:08:26

Curl proxy with variables from PHP file?

Hello everyone, please help me.
I need to connect to the target site through a proxy server, these proxy servers should be taken as a list from a file, there will be several of them. I roughly estimate that this is done through curl, proxy and fopen, but no matter how hard I try, nothing happens.
I have attached the code I wrote.

<?php
  if ($ch=curl_init())
  {
  curl_setopt($ch, CURLOPT_URL, 'ссылка');
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  curl_setopt($ch, CURLOPT_PROXY, $x:$y);
    $x=fopen("C:\Users\...\ip.txt", "r"); //айпи прокси
      if ($x) 
      while (($line=fgets($x)) !==false) {
        }
        fclose($x);
      } else {
      }
    $y=fopen("C:\Users\...\port.txt", "r"); //порты прокси
    if ($y) 
    while (($line=fgets($y)) !==false) {
        }
        fclose($y);
      } else {
      }
  curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_FAILONERROR, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 3);
  $result=curl_exec($ch);
  echo $result;
  }
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Softer, 2018-11-13
@Softer

First, we write the proxies.txt file in the IP:PORT format. One line - one connection proxy port

<?php
  if ($ch=curl_init())
  {
  curl_setopt($ch, CURLOPT_URL, 'ссылка');
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  $proxies = file ("C:\Users\...\proxies.txt"); // Кстати, я не уверен что в венде так пути писать надо
  $proxy = $proxies[rand(0,count($proxies) - 1)]; // Берем случайный прокси. "-1" - потому что count вернет число элементов, а не последний индекс. Отсчет-то с 0 начинается.
  curl_setopt($ch, CURLOPT_PROXY, "$proxy"); // Это надо делать ПОСЛЕ присваивания переменной $proxy значения. Ибо если делать перед - что в ней должно быть? Правильный ответ - ничего :)
  curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_FAILONERROR, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 3);
  $result=curl_exec($ch);
  echo $result;
  }
?>

Something like this. It was written on the knee, I could make a mistake somewhere.
PS: You should also read the basics :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question