循环PHP /爆炸

时间:2019-03-07 02:04:01

标签: php php-curl

晚上好

我正在创建一个脚本来检查代理。 但是,我对PHP的循环有所了解,我会尽力..

代码如下:

<?php 

//$_POST INFORMATIONS
$address = $_POST['address'];

if (!empty($_POST['address']))
{

//COMPTE LE NOMBRE D'ENTREES
$delimiter = $address;
$delimiterArray = ($delimiter != '')?explode(",",$delimiter):NULL;
$arrayCount = count($delimiterArray);

for ($i = 1; $i <= $arrayCount; $i++) {

$url = 'http://api.proxyipchecker.com/pchk.php';

//DELIMITE L'IP ET PORT
$format = explode(":", $address);
$ip = $format[0];
$port = $format[1];

//CURL
$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,'ip='.$ip.'&port='.$port);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

list($res_time, $speed, $country, $type) = explode(';', curl_exec($ch));

//REMPLACE LE RESULT
if (isset($type))
{
    if ($type >= 4 or $type <= 0)
    {
       $type = "Undefined";
    }
    elseif ($type = 1)
    {
       $type = "Transparent";
    }
    elseif ($type = 2)
    {
       $type = "Anonymous";
    }
    elseif ($type = 3)
    {
       $type = "High anonymous";
    }
}

//ECHO RESULT

echo $ip.":".$port." / Response time: ".$res_time." seconds / Country ".$country." / Type ".$type."\n";

 }
}

错误在这里

//DELIMITE L'IP ET PORT
$format = explode(":", $address);
$ip = $format[0];
$port = $format[1];

由于它是一个循环放置[0],[1],因此对于第一个循环将是有益的,然后将成为偏移量.. 如果有人有主意,非常感谢!

信息: 格式-> IP:PORT,IP:PORT ..

我需要为cURL的每个地址提供IP和PORT。

谢谢!

1 个答案:

答案 0 :(得分:0)

破坏了您想做的事情,您的循环逻辑有些偏离,并且对于您想要的(我认为)来说太复杂了。

...

//COMPTE LE NOMBRE D'ENTREES
//THIS IS THE FULL STRING (IP:PORT,IP:PORT,...)
$delimiter = $address;

//THIS IS AN ARRAY OF FORM 0 => "IP:PORT", 1 => "IP:PORT", etc. 
$delimiterArray = ($delimiter != '')?explode(",",$delimiter):NULL;

//ARRAYS ARE ITERABLE; USE FOREACH TO TRAVERSE EACH ENTRY

foreach ($delimiterArray as $value){
  $value = explode(":", $value);
  //THE COPY INSIDE OF VALUE IS NOW AN ARRAY;
  //$value[0] IS THE IP
  //$value[1] IS THE PORT
  //NOW DO WHAT YOU WANT WITH THOSE VALUES

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,'ip='.$value[0].'&port='.$value[1]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

list($res_time, $speed, $country, $type) = explode(';', curl_exec($ch));

//REMPLACE LE RESULT
if (isset($type))
{
    if ($type >= 4 or $type <= 0)
    {
       $type = "Undefined";
    }
    elseif ($type = 1)
    {
       $type = "Transparent";
    }
    elseif ($type = 2)
    {
       $type = "Anonymous";
    }
    elseif ($type = 3)
    {
       $type = "High anonymous";
    }


}//end foreach

您可能还希望在完成之后关闭每个curl会话(curl_close

相关问题