【问题标题】:Restart a PHP function with the value of the returned variable使用返回的变量的值重新启动 PHP 函数
【发布时间】:2015-12-23 15:30:56
【问题描述】:

我正在使用代理。无论出于何种原因,假设代理失败,我假设如果返回 403;我想用另一个(来自数组)替换代理。我不确定如何实现它。假设函数顶部有一个代理数组,称为代理

public static function get_http_response_code($url, &$redirect = null, $proxy = '23.244.68.94:80') {
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)) return false;

    if (!is_null($proxy)){

        $useragent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_PROXY, $proxy);
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        $header = curl_exec($ch); 
        curl_close($ch);
    } 

    // Pattern to find the status code
    $codepattern = '/[0-9]{3}/';
    preg_match($codepattern, $header, $codematch);

    // Pattern to find the redirect link
    $linkpattern = '/https?:\/\/(.+)\//';
    preg_match($linkpattern, $header, $linkmatch);

    // Store results in an array
    $statuscode = (array_values($codematch)[0]);
    // Store the redirect link in the $redirect variable
    if ($statuscode == 301 || $statuscode == 302 || $statuscode == 303) {
        if (strpos(array_values($linkmatch)[0], 'http') !== false) {
            $redirect   = array_values($linkmatch)[0];
        } else {

        }
    } 

    return $statuscode;  
}

$statuscode 将返回代码。如果是 403,我想从数组中获取下一个代理并重新启动函数。我正在考虑做$proxy = next($proxies);,但不确定在哪里添加这个

【问题讨论】:

  • 小心调用自己的函数。
  • @ODelibalta 我了解递归函数,但这不仅仅是调用自身的问题,因为该值设置在顶部,虽然变量在末尾有一个新值,但它被重新定义为原值一次调用?不?我很想知道是否有一些解决方法
  • 我敢打赌,如果您进一步充实您的问题,您会得到一个很好的答案。例如,也许编写一个递归函数,它可以做你想做的,但不能很好地工作,并寻求帮助来完成它。就像现在一样,你的最终目的还不是很清楚。
  • 您本可以这样做并亲自查看最终结果,而不是花时间提出理论问题来问我。只需使用您想要调用它的任何值调用您的函数。 $name=$name."y"; function_name($name)

标签: php function curl proxy


【解决方案1】:

我认为您最好的解决方案可能是在 foreach 循环中简单地使用您的函数:

$proxies = array(/*with stuff in it*/);
$url = 'my url';
$redirect = null;

foreach ($proxies as $proxy) {
    $statuscode = get_http_response_code($url, $redirect, $proxy);

    // If successful, break out of the foreach loop.
    // Otherwise, the loop will continue to the next proxy.
    if ($statuscode != 403)
        break;
}

【讨论】:

  • 正是我要找的东西,感谢帮助的伙伴!为之前的混乱道歉!
【解决方案2】:

好的,当我开始回答这个问题时,有人问了一些完全不同的问题,就像提供的示例一样。因此,我的答案不再完全有效,但它可能包含对您有用的信息,所以我还是把它留在这里。

在我看来,您的问题是范围界定问题。由于 $name 对您的函数是本地的,因此它回退到函数外部定义的值,因为函数的本地变量在函数结束时被销毁。

在下面的例子中,$name 是函数的一个全局变量,因此当函数内部发生变化时,它的值会保留在函数外部。

function addtekst() {
  global $name;
  $name = $name . "y";
}
$name = "Adam";
echo $name . " is 22 years old";
addtekst();
addtekst();
echo "<br>" . $name . " is 22 years old";

输出:
Adam 22 岁
Adamyy 22 岁

对于你当前的问题,我建议你使用mopo922提供的解决方案。

【讨论】:

  • 感谢 Van Dorsten 的贡献,为之前的混乱道歉!
【解决方案3】:
$name = "Adam";
$break = 0;
function add_y($name, $break) {
    echo $name . " is 22 years old";
    $name .= "y";
    /* we need to check a condition so that function dont run for infinite time so we used $break variable and after calling add_y five time we get out of the function */
    $break++;
    if ($break == 5) {
        exit;
    }
    add_y($name, $break);
}

add_y($name, $break);

谁投了反对票,请说明原因,以便我下次小心。 ?

【讨论】:

  • 我同意,请解释下投票,尤其是对新成员。
猜你喜欢
  • 1970-01-01
  • 2019-01-31
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
  • 2011-01-01
  • 1970-01-01
相关资源
最近更新 更多