2015-12-23 61 views
-1

我正在使用代理。無論出於何種原因,假設代理失敗,並且假設這是403返回的;我想用另一個(來自數組)替換代理。我不能確定如何實現它。假設有在函數的頂部代理陣列稱爲代理使用返回變量的值重新啓動PHP函數

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);的只是不確定在哪裏添加此

+1

讓它靜態的。 – Rizier123

+1

[遞歸函數](http://stackoverflow.com/questions/2648968/what-is-a-recursive-function-in-php) – ODelibalta

+0

小心使用自己調用的函數。 – crmpicco

回答

0

我覺得你最好的解決方案可能是簡單地一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; 
} 
+0

準確地說,我正在尋找莫,我欣賞幫助哥們!對早先的困惑道歉! – Nouman

-1
$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); 

;誰投下來請註明原因,這樣我可能下次小心。 ?

+0

我同意,請解釋downvote,特別是對一個新成員。 – mopo922

0

好的,當我開始回答這個問題時,問了一個完全不同的東西,就像例子提供的那樣。因此,我的答案不再是完全有效的,但它可能包含有用的信息,因此無論如何我都會將其留在這裏。

因爲它聽起來你的問題是一個範圍界定問題。由於$ 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"; 

輸出:
亞當是22歲
Adamyy是22歲

當涉及到當前的問題,我建議你去與mopo922提供的解決方案。

+0

感謝您對Van Dorsten的貢獻,對早先的困惑道歉! – Nouman