2011-09-06 82 views
0

只有一個問題。我得到一個基於cURL的代碼,它發送一個請求到serwer,然後如果響應是'有效的'它是在做一個sql查詢,但是如果響應'忙',我需要更改腳本正在使用的代理。CURL響應的數據更改PHP

我做這樣說:

$proxys = file('http_proxy.txt'); 
...then... 
for($n = 0, $count = count($proxys); $n <= $count; $n++) { 
...and to change the proxy I used something like this: 
$proxy = $proxys[$n + 1]; 

,但它不工作。

有什麼建議嗎?

問候。

+0

既然你已經有一個'for',在'$代理= $ proxys [$ N + 1]循環他們。 '應該是多餘的 - 你應該寫下來,以便你可以['繼續;](http://php.net/manual/en/control-structures.continue.php)代替... – DaveRandom

回答

1

對於初學者,file('http_proxy.txt');將在您的文件中保留換行符,因此使用FILE_IGNORE_NEW_LINES標誌省略此項。然後,你可以使用break;成功地使用上的代理捲曲後停止循環:

$proxys = file('http_proxy.txt', FILE_IGNORE_NEW_LINES); 
foreach($proxys as $proxy) 
{ 
    $response = sendRequestTo($proxy); 
    if($response == 'valid') 
    { 
     performQuery($proxy); 
     break; 
    } 
}