如何从外部网站抓取内容片段并将其显示在我的网站上?(类似于 RSS 提要或其他聚合器所做的事情)。
例如,假设我想显示另一个网站日历中的项目:
其他网站:
<h1>Here's our calendar:</h1>
<div class="calendar_item">
<h2>Boston Marathon</h2>
<p class="date">June 23, 2012</p>
<p class="description">This marathon is 26.2 miles and lots of fun.</p>
</div>
<div class="calendar_item">
<h2>Irish Pub Crawl</h2>
<p class="date">July 17, 2012</p>
<p class="description">Shamrocks and green things are super-fun.</p>
</div>
<div class="calendar_item">
<h2>Tim's Birthday</h2>
<p class="date">August 25, 2012</p>
<p class="description">It's Tim's birthday, yo.</p>
</div>
我的网站:
<h1>Here's a feed of some calendar items from someone else's website:</h1>
<div class="event_title">Boston Marathon</div>
<div class="event_date">June 23, 2012</div>
<div class="event_description">This marathon is 26.2 miles and lots of fun.</div>
<div class="event_title">Irish Pub Crawl</div>
<div class="event_date">July 17, 2012</div>
<div class="event_description">Shamrocks and green things are super-fun.</div>
<div class="event_title">Tim's Birthday</div>
<div class="event_date">August 25, 2012</div>
<div class="event_description">It's Tim's birthday, yo.</div>
这是我尝试过的(使用 MAMP):
<?php
$url = "http://example.com";
$page = curl($url);
$pattern = '%
<h2>(.+?)</h2>
%i';
preg_match($pattern,$page,$matches);
print_r($matches);
?>
...打印:
Array ( )
教程/等。我查看过包括“尝试 cURL”之类的模棱两可的答案。这看起来很简单,但我是一个难过的菜鸟。
提前谢谢各位:)