【问题标题】:Accessing URL within Twitter XML Tag访问 Twitter XML 标签中的 URL
【发布时间】:2012-02-06 05:54:56
【问题描述】:

我目前正在通过地理编码解析 Twitter 推文并使用 php 显示它们。但目前无法访问该推文的 URL,而是选择向用户显示 Twitter 个人资料。

我尝试访问的 URL 在链接标签内

<link type="text/html" href="http://twitter.com/pmhigham/statuses/166271863331368961"     rel="alternate" >

它在 XML 文件中的位置是 Feed->Entry->Link。

我曾想过使用正则表达式,但不知道如何使用它。

这是我的代码。

<h3><?php
if (isset($column1Heading)) {
echo $column1Heading;
}
?></h3>
                <p>
                <strong>Tweets within 10 miles radius of London Eye</strong></br>
              <?php 
               $feed = simplexml_load_file ('http://search.twitter.com/search.atom?geocode=51.5069999695%2C-0.142489999533%2C10.0mi%22london%22&lang=en&rpp=5');
                if ($feed){foreach ($feed->entry as $item) {

                      echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
                }
                }
                else echo "Cannot find Twitter feed!"
                ?>
                </br>
               <strong>London Hashtag</strong>
               </br>
                 <?php 
               $feed = simplexml_load_file ('http://search.twitter.com/search.atom?q=%23london&lang=en&rpp=5');
                if ($feed){foreach ($feed->entry as $item) {

                      echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
                }
                }
                else echo "Cannot find Twitter feed!"
                ?>


            </p>
        </li>
        <li class="col2">
            <h3><?php
                if (isset($column2Heading)) {
                    echo $column2Heading;
                }
                ?></h3>
            <p>
               <?php 
               $feed = simplexml_load_file ('http://search.twitter.com/search.atom?geocode=40.744544%2C-74.027593%2C10.0mi%22manhattan%2C+ny%22&lang=en&rpp=5');
                if ($feed){foreach ($feed->entry as $item) {

                      echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
                }
                }
                else echo "Cannot find Twitter feed!"
                ?>
                <strong>NYC Hashtag</strong>
               </br>
                 <?php 
               $feed = simplexml_load_file ('http://search.twitter.com/search.atom?q=%23nyc&lang=en&rpp=5');
                if ($feed){foreach ($feed->entry as $item) {

                      echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
                }
                }
                else echo "Cannot find Twitter feed!"
                ?>

            </p>
        </li>
        <li class="col3">
            <h3><?php
                if (isset($column3Heading)) {
                    echo $column3Heading;
                }
                ?></h3>
            <p>
                <?php 
               $feed = simplexml_load_file ('http://search.twitter.com/search.atom?lang=en&geocode=48.861%2C2.336%2C5.0mi%22paris%2C+fr%22&rpp=5&lang=en');
                if ($feed){foreach ($feed->entry as $item) {

                      echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
                }
                }
                else echo "Cannot find Twitter feed!"
                ?>

            </p>
        </li>
    </ul>
    <div class="clear"></div>
</div>
<?php require_once 'footer.php'; ?>

【问题讨论】:

    标签: php xml twitter


    【解决方案1】:

    如果我理解正确的话:

    您可以使用 SimpleXML 的内置attributes() method 访问该属性。

    $item->link->attributes()->href
    

    否则正则表达式应该是这样的

    $string = '<link type="text/html" href="http://twitter.com/pmhigham/statuses/166271863331368961"     rel="alternate" >';
    preg_match('/href\=\"([^\"]+)\"/i', $string, $matches);
    

    【讨论】:

      猜你喜欢
      • 2018-12-23
      • 1970-01-01
      • 2011-04-13
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 2022-10-06
      • 2011-09-20
      • 1970-01-01
      相关资源
      最近更新 更多