我使用php脚本使用curl登录网页,工作正常。然后我再加载两页并将它们存储到变量中。
curl_setopt($ch, CURLOPT_URL, $page1);
$resources = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $page2);
$installations = curl_exec($ch);
我使用两个函数来处理$ page1和$ page 2中的数据
//Function findResourcesBuildings. Requires $page1
function findResourcesBuildings($somepage) {
for ($i=1; $i < 10 ; $i++) {
$button = $i;
$html = new simple_html_dom();
$html->load($somepage, true, false);
$result_tmp = preg_match_all('!\d+!', $html->find("li[id=button".$button."] span.level",0)->plaintext, $matches);
$level = $matches[0][0];
$state_char = $html->find("li[id=button".$button."]",0)->getAttribute('class');
$state_title = $html->find("li[id=button".$button."] a",0)->title;
$state = buildState($state_char,$state_title);
$result[]=array('lvl'=>$level,'state'=>$state);
}
return $result;
}
//Function findInstallationsBuildings. Requires $page2
function findInstallationsBuildings($somepage) {
for ($i=0; $i < 7 ; $i++) {
$button = $i;
$html = new simple_html_dom();
$html->load($somepage, true, false);
$result_tmp = preg_match_all('!\d+!', $html->find("li[id=button".$button."] span.level",0)->plaintext, $matches);
$level = $matches[0][0];
$state_char = $html->find("li[id=button".$button."]",0)->getAttribute('class');
$state_title = $html->find("li[id=button".$button."] a",0)->title;
$state = buildState($state_char,$state_title);
$result[]=array('lvl'=>$level,'state'=>$state);
}
return $result;
}
$test = findResourcesBuildings($page1) //Works
$test2 = findInstallationsBuildings($page2) // error: Call to a member function getAttribute() on a non-object
或
$test = findInstallationsBuildings($page2) //Works
$test2 = findResourcesBuildings($page1) // error: Call to a member function getAttribute() on a non-object
错误在这一行:
$state_char = $html->find("li[id=button".$button."]",0)->getAttribute('class');
但正如我上面所述,无论顺序如何,第二个变量都失败了。
可能会发生什么?我迷失在这里。 感谢