我從使用PHP簡單的HTML DOM解析器的目錄號碼提取一些來自宜家網站的元數據。在使用PHP處理HTML時不時發生致命錯誤簡單的HTML DOM解析器
號30275861和其他我正確的測試工作,併爲因此給該鏈接($ PRODUKT變量)數十一些數據http://www.ikea.com/pl/pl/catalog/products/30275861/?query=30275861(如果鏈接粘貼到瀏覽器,它給出了卡拉克斯系統傢俱頁)
給數69136138 - 鏈接的結果($ PRODUKT變量)http://www.ikea.com/pl/pl/catalog/products/S69136138/?query=69136138如果粘貼到瀏覽器(無敵電視傢俱)的作品給出了錯誤:
致命錯誤:調用一個成員函數find()方法上,在大多數作品布爾
代碼案例如下所示:
<?php
include('simple_html_dom.php');
function clean($string) {
$string = str_replace(',', '.', $string);
return preg_replace('/[^A-Za-z0-9\-.]/', '', $string);
}
if(isset($_POST['produkt_id'])){
$produkt_id=str_replace('.', '', $_POST['produkt_id']);
$url="http://www.ikea.com/pl/pl/search/?query=".$produkt_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch); // $a will contain all headers
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // Return the last effective URL
$produkt=(string)$url;
$html = file_get_html($produkt);
echo $produkt_id;
echo "<br>";
echo $produkt;
foreach($html->find('meta[name=partnumber]') as $e) echo $kod=$e->content;
foreach($html->find('link[rel=image_src"]') as $e) echo $obrazek=$e->href;
foreach($html->find('meta[name=title]') as $e) echo $nazwa=$e->content;
foreach($html->find('meta[name=price]') as $e) echo $cena=floatval(clean($e->content));
?>
什麼是'file_get_html'? – Dekel
@Dekel'file_get_html'是'simple_html_dom'類的一個方法... ;-) – Poiz
因爲它不是標準的PHP類/方法,所以你應該在你的文章中提及它(鏈接到lib應該提供)。 – Dekel