简体   繁体   中英

PHP undefined index on first request

I have a script that uses regex to get some value on a page retrieved with curl.

the code: preg_match('/<h2>([\\d\\,]+)<\\/h2>/', $curl_exec, $matches);

When I print_r($matches) in my browser, it's always there.

I'm using this script in a cron job to get the results every 10 minutes. when I test it myself and use 'php script.php', it always says undefined index for the data i want to get, but if i do 'php script.php' a second time, it works. Does anyone know what could be causing this? Any work arounds?

$CurlHandle = curl_init($StatsUrl);
curl_setopt($CurlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($CurlHandle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($CurlHandle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Firefox/17.0");

$Data = curl_exec($CurlHandle);

preg_match("/<h2>([\d\,]+)<\/h2>/", $Data, $Matches);

print_r($Data);

if(!isset($Matches[1])) exit('Irrecoverable error.');

$PlayerCount = str_replace(",", "", $Matches[1]);

$CurrentTime = time();

Don't know what was originally causing the problem, but I added a system('php script.php'); in the event of failure, so it calls it twice. This is obviously a duct tape solution, but it's working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM