![](/img/trans.png)
[英]load external webpage and add custom header and use the data from webpage
[英]Getting data from an external webpage
通過php從外部網站獲取內容的最佳方法是什么?
使用php如何訪問網頁(例如: http : //store.domain.com/1/ )並掃描HTML編碼以查找介於兩者之間的數據(即字母C和E)。 我使用什么php方法?
<span id="ctl00_ContentPlaceHolder1_phstats1_pname">C</span>
<span id="ctl00_ContentPlaceHolder1_phstats2_pname">E</span>
然后將“ C”(找到的字符串)保存到$ pname
$_session['pname1'] = $pname1;
$_session['pname2'] = $pname2;
最有效的方法是:
$content = file_get_contents('http://www.domain.com/whatever.html');
$pos = str_pos($content,'id="c');
$on=0;
while($pos!==false)
{
$content = substr($content,$pos+4);
$pos = str_pos($content,'"');
$list[$on] = substr($content,0,$pos);
$on++;
$pos = str_pos($content,'id="c');
}
然后,您所有的值都將在$ list數組中,其計數為$ on。
您也可以使用preg函數之一來完成它,但是我喜歡老式的方法,它的速度要快十億分之一秒。
我認為您實際上可以使用file_get_contents("http://store.domain.com/1/");
進行http請求。
就解析而言,根據您的項目規模和您願意付出的努力,您可以在此處找到一個html DOM解析器,例如http://simplehtmldom.sourceforge.net/或僅搜索id="ctl00_ContentPlaceHolder1_phstats1_pname"
,並將其id="ctl00_ContentPlaceHolder1_phstats1_pname"
(不是推薦的處理方式)。
可以通過CURL完成。 但是,您可以僅在項目中包括簡單HTML DOM解析器。 它非常易於使用,將滿足您的目的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.