![](/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.