真正的問題域是,如何使用UTF-8字符的URL而不是simple_html_dom
檢索數據。
首先,我們需要編碼字符:
$url_link = 'http://kissanime.com/Anime/Knights-of-Ramune-VS騎士ラムネ&40FRESH';
$strPosLastPart = strrpos($url_link, '/') + 1;
$lastPart = substr($url_link, $strPosLastPart);
$encodedLastPart = rawurlencode($lastPart);
$url_link = str_replace($lastPart, $encodedLastPart, $url_link);
Normaly這應該工作。由於我測試了它,它不起作用。所以我在問爲什麼會發生這種錯誤,並使用CURL進行了一次呼叫。
未將對象引用設置爲對象的實例。說明:執行當前Web 請求期間發生未處理的異常 。請查看堆棧跟蹤以獲取有關 錯誤以及源代碼的更多信息。
異常詳細信息:System.NullReferenceException:對象引用不是 設置爲對象的實例。
現在我們知道,這個頁面是用ASP.NET編寫的。但我問我,爲什麼它不工作。我添加了一個用戶代理,瞧:
$ch = curl_init($url_link);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0');
$data = curl_exec($ch);
echo $data;
所有在一起(工作):
$url_link = 'http://kissanime.com/Anime/Knights-of-Ramune-VS騎士ラムネ&40FRESH';
//Encode Characters
$strPosLastPart = strrpos($url_link, '/') + 1;
$lastPart = substr($url_link, $strPosLastPart);
$encodedLastPart = rawurlencode($lastPart);
$url_link = str_replace($lastPart, $encodedLastPart, $url_link);
//Download Data
$ch = curl_init($url_link);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0');
$data = curl_exec($ch);
//Load Data into Html (untested, since i am not using this Lib)
$html = str_get_html($data);
現在的區別是,讀$data
到您的simple_html_dom.php
類,而不是file_get_html
。
乾杯
您應該回顯$ html以查看您是否正確檢索頁面。 – 2014-09-01 15:40:11
它只是將我重定向到域名只有 – 2014-09-01 15:51:48
然後問題不是與simple_html_dom,而是與file_get_html。我懷疑網址是否允許包含特殊字符,但我沒有參考資料。 – 2014-09-01 15:59:40