我有脚本检查袜子的东西都准备就绪,但当它的袜子死了没有改变尝试从列表中的其他袜子,看下面,请帮助任何人解决这个问题:
$ch = curl_init();
if($sock!=''){
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
curl_setopt($ch, CURLOPT_PROXY, $sock);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 25);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dattt);
//curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
答案 0 :(得分:0)
尝试这样的事情
<?php
$hosts = array(
'xx.com',
'yy.com'
);
class TimeoutException extends \Exception {
}
class Curl
{
public function connect($sock, $ch, $dattt, $url)
{
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
curl_setopt($ch, CURLOPT_PROXY, $sock);
curl_setopt($ch, CURLOPT_TIMEOUT, 25);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dattt);
// etc
$result = curl_exec($ch);
// 28 is curl timeout
if (curl_errno($ch) === 28) {
throw new TimeoutException;
}
// some other error handling...
return $result;
}
}
$curl = new Curl;
foreach ($hosts as $socks) {
try {
$result = $curl->connect($socks, $curlResouece, $dattt, $url);
break;
} catch (TimeoutException $e) {
continue;
}
}
if (!isset($result)) {
die('Failed all socks');
}
echo $result;
至于解释,您将循环遍历您指定的socks代理。如果CURL错误是28,它将抛出你在try catch块中捕获的超时异常。然后,您可以继续使用下一个代理。如果有结果,它将脱离foreach并立即返回结果。如果所有的socks代理都不可用,那么它将会出错。