samedi 9 mai 2015

How to str_replace Google News RSS for Facebook Share?

Hi I'm using simpleXML to display a news.google.com feed.

The displayed entries link to the original article in this way:

http://ift.tt/1dTSQsr

I need the entries to link to this instead: http://ift.tt/1KujlPl

The reason is that Facebook Sharer cannot interpret the following link:

http://ift.tt/1dTSQst

Facebook Sharer needs it to look like this:

http://ift.tt/1dTSQsv

Is there a way that I can use regex (str_replace or preg_match) to remove the Google redirect URL so that social sharing sites can recognize the link?

The Google redirect URL is dynamic and so it will be slightly different each time and so I will need something that can replace each variant.

My working, functional code:

    $feed = file_get_contents("http://ift.tt/1dTSQIN");
$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;
  }

I'm using $desc to display the link instead of $link, but URL to the article with the Google redirectURL is still in $link if you would like to str_replace or preg_match $link instead of $desc

Link to working Google News feed below: http://ift.tt/1dTSQIN

If you know how to fix this you're a hero. Thank you Overflowers

Aucun commentaire:

Enregistrer un commentaire