While loop does not work well if first value aof array is not correct php
I’m using serp spider to check if proxy is available. Here is the code:
public function checkGoogle(ProxyModel $proxyModel, Keywords_Info $kinfo)
{
$pr = Keywords_Info::where('user_id', '=', auth()->user()->id)->get();
foreach ($pr as $user_proxy) {
if ($user_proxy->google == 'SUCCESS' || $user_proxy->google == 'FAIL')
{
}
else
{
do {
$respo = 'FAIL';
$proxy_ip = array( '11.11.11.11', '22.22.22.22', '33.33.33.33');
$proxy_rand = rand(0,2);
$proxy_ip = $proxy_ip[$proxy_rand];
$type = 'HTTP';
$port = 80;
$puser = 'username';
$ppass = 'password';
$proxies = new Proxy($proxy_ip, $port, $puser, $ppass, $type);
$userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36";
$browserLanguage = "en";
$browser = new Browser(new CurlClient(), $userAgent, $browserLanguage);
$browser->setProxy($proxies);
$browser->setAcceptLanguage('en');
$googleClient = new GoogleClient($browser);
$googleUrl = new GoogleUrl('google.com');
try {
$response = $googleClient->query($googleUrl);
$respo = 'SUCCESS';
DB::table('keywords_info')->where('user_id', auth()->user()->id)->where('proxy', '=', '-')->update(['proxy' => $proxy_ip]);
DB::table('keywords_info')->where('user_id', auth()->user()->id)->where('proxy', '=', $proxy_ip)->update(['google' => 'SUCCESS']);
echo 'success';
break;
} catch (Exception $e) {
$respo = 'FAIL';
$respo_items = [
'user_id' => '1',
'keywords' => $user_proxy->keywords,
'proxy' => $proxy_ip,
'google' => 'FAIL',
'yahoo' => '-',
'bing' => '-'
];
Logs::create($respo_items);
echo 'fail';
}
} while ($respo != 'FAIL');
}
}
}
This works if first proxy 11.11.11.11 is ok, if that proxy fails script breaks, while loop does not continue to check another proxy. Only if first one fails loop breaks, if any other fail that’s work.
from Laravel Questions and Answers https://laravelquestions.com/php/while-loop-does-not-work-well-if-first-value-aof-array-is-not-correct-php/
via Lzo Media
No comments:
Post a Comment