dongwen7371 2012-05-07 17:52
浏览 36
已采纳

如何显示从外部网站获取的内容?

How do I grab pieces of content from external websites and display them on my website? (Similar to what an RSS feed or other aggregator does).

For example, say I want to display items from another website's calendar:

Other website:

<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>

My website:

<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>

Here's what I've tried (using MAMP):

<?php

$url = "http://example.com";

$page = curl($url);

$pattern = '%
<h2>(.+?)</h2>
%i';

preg_match($pattern,$page,$matches);

print_r($matches);

?>

...which prints:

Array ( )

The tutorials/etc. I've viewed include ambiguous answers like "try cURL". This seems so simple, but I'm a stumped noob.

Thanks in advance, guys :)

  • 写回答

3条回答 默认 最新

  • doubijiao2094 2012-05-07 18:34
    关注

    I would not recommend regex for parsing HTML. PHP 5+ comes with a parser which you can use as shown below.

    $content = file_get_contents('test.html');
    $doc = 
    <<<DOC
    $content
    DOC;
    $dom = new DOMDocument();
    $dom->loadHTML($doc);
    $h2Tags = $dom->getElementsByTagName("h2");
    $pTags = $dom->getElementsByTagName("p");
    foreach($h2Tags as $h2 ) {
        //do something
    }
    
    foreach($pTags as $p ) {
    if($p->getAttribute("class") == "date") {
        //do something
    }
    
    }
    

    $h2 is of type DOMElement. It inherits DOMNode. So you can use nodeValue property to access the values. In the above example, you can write $h2->nodeValue to access the content.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用