【问题标题】:How to str_replace Google News RSS for Facebook Share?如何为 Facebook 分享 str_replace 谷歌新闻 RSS?
【发布时间】:2015-07-18 17:13:26
【问题描述】:

您好,我正在使用 simpleXML 来显示 news.google.com 供稿。

显示的条目以这种方式链接到原始文章:

http://news.google.com/news/url?sa=t&fd=R&ct2=us&usg=AFQjCNEcqhcp4AfUzgxc2l1gumydaxQ-KQ&clid=c3a7d30bb8a4878e06b80cf16b898331&cid=52778832126843&ei=keFLVfiHGvDVmQL5_4GgBg&url=http://WEBSITEWITHNEWS.COM/ARTICLEURLHERE

我需要条目来链接到这个: http://WEBSITEWITHNEWS.COM/ARTICLEURLHERE

原因是 Facebook Sharer 无法解释以下链接:

https://www.facebook.com/sharer/sharer.php?u=http://news.google.com/news/url?sa=t&fd=R&ct2=us&usg=AFQjCNEcqhcp4AfUzgxc2l1gumydaxQ-KQ&clid=c3a7d30bb8a4878e06b80cf16b898331&cid=52778832126843&ei=keFLVfiHGvDVmQL5_4GgBg&url=http://WEBSITEWITHNEWS.COM/ARTICLEURLHERE

Facebook Sharer 需要它看起来像这样:

https://www.facebook.com/sharer/sharer.php?u=http://WEBSITEWITHNEWS.COM/ARTICLEURLHERE

我是否可以使用 regex(str_replace 或 preg_match) 删除 Google 重定向 URL,以便社交共享网站可以识别该链接?

Google 重定向 URL 是动态的,因此每次都会略有不同,因此我需要一些可以替换每个变体的东西。

我的工作,功能代码:

    $feed = file_get_contents("https://news.google.com/news/feeds?q=KEYWORD&output=rss");
$xml = new SimpleXmlElement($feed);
foreach ($xml->channel->item as $entry){
  $date = $entry->pubDate; 
  $date = strftime("%m/%d/%y %I:%M:%S%P", strtotime($date));
  $desc = $entry->description;
  $desc = str_replace("and more »", "","$desc");
  $desc = str_replace("font-size:85%", "font-size:100%","$desc");
  ?>
  <div class="item"></div>
  <?php echo $desc; ?>
  <div class="date">
  <?php echo $date; ?></div>
  <?php } ?>
 $desc = $entry->description;
 $date = $entry->pubDate; 
 $date = strftime("%A, %m/%d/%Y, %H:%M:%S", strtotime($date));
 $desc = str_replace("and more »","x","and more »");
  echo $date; 
  echo $desc;
  }

我使用 $desc 而不是 $link 来显示链接,但是如果您想使用 str_replace 或 preg_match $link 而不是 $desc,则使用 Google 重定向 URL 的文章的 URL 仍然在 $link 中

以下链接到有效的 Google 新闻提要: https://news.google.com/news/feeds?q=KEYWORD&output=rss

如果您知道如何解决此问题,您就是英雄。谢谢溢出者

【问题讨论】:

  • 所以在$desc,如果你有href="http://news.google.com/news/url?sa=t&amp;amp;fd=R&amp;amp;ct2=us&amp;amp;usg=AFQjCNHG6ECOl4eSYKUWwztaiXMvxZGi1A&amp;amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;amp;cid=52778831966961&amp;amp;ei=oRRMVfjOLNqs3AGijoDoCQ&amp;amp;url=http://www.wordstream.com/blog/ws/2015/05/06/adwords-shortcuts",你会想要href="https://www.facebook.com/sharer/sharer.php?u=http://www.wordstream.com/blog/ws/2015/05/06/adwords-shortcuts"吗?
  • 嗯,是的,有点。我希望能够让人们分享这篇文章。如果您使用 $desc 按照您所说的方式编写它,那就太好了。但是,如果我可以删除 $link 中的重定向 url,那就更好了。
  • 重定向 URL 是前面的 &amp;amp;url=?
  • 是的,就像你说的那样。我想删除整个 URL,包括 &url=。所以 all(.*) 直到第二个 http://

标签: regex rss preg-match simplexml str-replace


【解决方案1】:

您可以为此使用内置的 PHP 函数 parse_url(将 URL 拆分为组件)和 parse_str(从查询字符串中获取参数值):

$feed = file_get_contents(
    "https://news.google.com/news/feeds?q=KEYWORD&output=rss"
);
$xml = new SimpleXmlElement($feed);

foreach ($xml->channel->item as $entry){
    // Get query part of link
    $query = parse_url($entry->link, PHP_URL_QUERY);

    // Parse query parameters into $params array
    parse_str($query, $params);

    // Get URL from parameters
    $url = $params['url'];

    // Just output in this example
    echo "URL: $url", PHP_EOL;

    // ... Do some more stuff
}

输出:

URL: http://www.gamasutra.com/blogs/JonathanRaveh/20150506/242840/Death_of_the_app_keyword__whats_next.php
URL: http://www.business2community.com/online-marketing/8-keyword-optimization-tips-perfect-ppc-campaigns-01222200
URL: http://searchengineland.com/marry-keywords-compelling-content-218174
...

【讨论】:

    【解决方案2】:

    我第一条评论的答案是使用这个正则表达式。

    <?php
    date_default_timezone_set('America/New_York');
    $feed = file_get_contents("https://news.google.com/news/feeds?q=KEYWORD&output=rss");
    $xml = new SimpleXmlElement($feed);
    foreach ($xml->channel->item as $entry) {
        $date = $entry->pubDate;
        $date = strftime("%m/%d/%y %I:%M:%S%P", strtotime($date));
        $desc = $entry->description;
        $desc = str_replace("and more&nbsp;&raquo;", "","$desc");
        $desc = str_replace("font-size:85%", "font-size:100%","$desc"); /*
        ?>
        <div class="item"></div>
        <?php // echo $desc; ?>
        <div class="date"><?php echo $date; ?></div>
        <?php
        */
        $desc = $entry->description;
        $desc = preg_replace('~href=".*?&amp;url=(.*?)"~', 'href="https://www.facebook.com/sharer/sharer.php?u=$1"', $desc);
        $date = $entry->pubDate; 
        $date = strftime("%A, %m/%d/%Y, %H:%M:%S", strtotime($date));
        //$desc = str_replace("and more »","x","and more »");
        echo $date . "\n" . $desc;
        die('1 pass');
    }
    ?>
    

    输出(为显示而改变的格式):

    <table border="0" cellpadding="2" cellspacing="7" style="vertical-align:top;">
        <tr>
            <td width="80" align="center" valign="top"><font style="font-size:85%;font-family:arial,sans-serif"></font></td>
            <td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br>
                <div style="padding-top:0.8em;"><img alt="" height="1" width="1"></div>
                <div class="lh"><a href="https://www.facebook.com/sharer/sharer.php?u=http://www.gamasutra.com/blogs/JonathanRaveh/20150506/242840/Death_of_the_app_keyword__whats_next.php"><b>Death of the app <b>keyword</b> – what&#39;s next?</b></a><br>
                    <font size="-1"><b><font color="#6f6f6f">Gamasutra (blog)</font></b></font><br>
                    <font size="-1">Yes, app <b>keywords</b> are dying. If you search the web you may find insightful stories about apps that gained massive recognition due to the clever use of <b>keywords</b>. Many companies and services (such as Sensor Tower) offer developers tools to help them&nbsp;...</font><br>
                    <font size="-1" class="p"></font><br>
                    <font class="p" size="-1"><a class="p" href="http://news.google.com/news/more?ncl=d4b6j-gMxFN1VKM&amp;authuser=0&amp;ned=us"><nobr><b>and more&nbsp;&raquo;</b></nobr></a></font></div>
                </font></td>
        </tr>
    </table>
    1 pass
    

    这个正则表达式".*?&amp;amp;url=(.*?)",在第一个双引号和最后一个href之间查找,并捕获&amp;amp;url=之后的所有内容。在示例中,我看到每个实例都将 URL 作为最后一个参数。如果 URL 不是最后一个参数,则此正则表达式将不起作用,因为它使用检查来查找最后一个双引号或实体 & 符号;那是("|&amp;amp;)。我可以看到从 URL 中截断参数;如果他们有额外的GET 参数。我从未在这些 URL 中看到的另一件事是它们使用 GET 参数。取出die('1 pass'); 并尝试一下,如果您一开始想要小样本,请保留die

    【讨论】:

    • 这太棒了,谢谢!工作得很好。如果不是因为 mhall 的回答,我会接受你的作为获胜的答案。但是我现在使用你的两个代码来编辑 $desc 和 $link。谢谢 chris85,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多