首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环PHP / Explode

循环PHP / Explode
EN

Stack Overflow用户
提问于 2019-03-07 10:04:01
回答 1查看 71关注 0票数 0

晚上好,

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

代码如下:

代码语言:javascript
复制
<?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";

 }
}

错误在这里

代码语言:javascript
复制
//DELIMITE L'IP ET PORT
$format = explode(":", $address);
$ip = $format[0];
$port = $format[1];

由于它是一个循环put,1它对第一个是好的,然后它将是一个偏移量。如果任何人有一个想法,非常感谢!

信息:格式-> IP:端口,IP:端口..

我需要为cURL的每个地址的IP和端口。

谢谢你!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-07 10:50:08

分解你正在尝试做的事情,你的循环逻辑有点离谱,对于你想要的(我认为)来说太复杂了。

..。

代码语言:javascript
复制
//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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55034942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档