Show Twitter Feed on my website - php

Up until a few weeks ago the below code worked great to grab my last three tweets and display them on my website. now it's not working. I've looked through Twitter's messages boards to see if something changed to no avail.
Does anyone know how to effectively display your latest tweets on a website using php?
my original code is here. Like I said, this worked up until a few weeks ago:
$twitterUsername = "myUsername";
$amountToShow = 3;
$twitterRssFeedUrl = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$twitterUsername.'&count='.$amountToShow;
$twitterPosts = false;
$xml = #simplexml_load_file($twitterRssFeedUrl);
if(is_object($xml)){
foreach($xml->channel->item as $twit){
if(is_array($twitterPosts) && count($twitterPosts)==$amountToShow){
break;
}
$d['title'] = stripslashes(htmlentities($twit->title,ENT_QUOTES,'UTF-8'));
$description = stripslashes(htmlentities($twit->description,ENT_QUOTES,'UTF-8'));
if(strtolower(substr($description,0,strlen($twitterUsername))) == strtolower($twitterUsername)){
$description = substr($description,strlen($twitterUsername)+1);
}
$d['description'] = $description;
$d['pubdate'] = strtotime($twit->pubDate);
$d['guid'] = stripslashes(htmlentities($twit->guid,ENT_QUOTES,'UTF-8'));
$d['link'] = stripslashes(htmlentities($twit->link,ENT_QUOTES,'UTF-8'));
$twitterPosts[]=$d;
}
}else{
die('Can`t fetch the feed you requested');
}
and then it turns up in the html like so:
<dl class="twitter">
<dt>Twitter Feed</dt>
<?php
if(is_array($twitterPosts)){
echo '';
foreach($twitterPosts as $post){
$data = hyperlinks($post['description']);
$data = twitter_users($data);
echo '<dd>'.$data.'. ';
echo 'Posted '.time2str(date($post['pubdate'])).'</dd>';
}
echo '';
}else{
echo 'No Twitter posts have been made';//Error message
}
?>
<dd>

Twitter API 1.0 that you're using has been switched off, as of a few weeks ago.
Read up on the API 1.1 here: https://dev.twitter.com/docs/api
There are tonnes of PHP libraries for working with the new API, including mine.

The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.

Related

How to determine whether my content has a instagram embed?

The posts in my site has video(single) from anyone of the following embeds.
Youtube
Facebook
Instagram
My question is while fetching them on front end I want to findo out whether my content has an embed, if so which of the following is embedded. (iframe presence checking is one (dirty)way still it own work for instagram)
PHPCODE:
$video_start = strpos($singlePost->post_content, "<iframe");//Get to the start of the iframe(video)
$video_stop = strpos($singlePost->post_content, "</iframe>");//Get to the end of the iframe(video)
$iframe_content = substr($singlePost->post_content, $video_start, $video_stop);
$xpath = new DOMXPath(#DOMDocument::loadHTML($iframe_content));
$iframe_src = $xpath->evaluate("string(//iframe/#src)");
$parsed_url = parse_url($iframe_src);
$host = $parsed_url['host'];
if(strpos($host, "youtube") !== false) { // If it is a youtube video append this
$iframe_src = $iframe_src."?rel=0";// This option has to be appended of youtube URL's
$related_social_icon = "youtube";
$related_social_media = "youtube";
}
<iframe class="<?php echo $iframe_class; ?>" src="<?php echo $iframe_src; ?>" style="background-size: cover;" allowfullscreen></iframe>
Above code works fine for youtube, but does not work for instagram coz when inserting instagram comes as blockquote tags,but if you echo them it will be straight away become iframe tags due to the script in it.
I would go for something like this:
add_filter('the_content', function($content) {
$identifier = '<embed';
if (strpos($content, $identifier) !== false) {
// identifier found
$content = '<h1>This page includes an embed</h1>'.$content;
}
return $content;
});
I'm not sure how your embeds look like, you are talking about iframes to. So you need to find some identifiers that you can check.
Your post probably got downvoted because it could have some more information?

Instagram Fetch Amount of Images API

A friend of mine wrote this script, displaying the 20 most recent instagram images, and I was wondering, how can I change the amount of images it grabs to maybe, 6?
<?PHP
$token = 'token';
$username = 'username';
$userInfo = json_decode(file_get_contents('https://api.instagram.com/v1/users/search?q='.$username.'&access_token='.$token));
if($userInfo->meta->code==200){
$photoData = json_decode(file_get_contents('https://api.instagram.com/v1/users/'.$userInfo->data[0]->id.'/media/recent/?access_token='.$token));
if($photoData->meta->code==200){ ?>
<?PHP foreach($photoData->data as $img){
echo '<img src="'.$img->images->thumbnail->url.'">';
} ?>
<?PHP } // If
} // If
?>
Now, the script is functional now because I've been working on it all day, but I'm not sure how to change how many it sends out.
Also, would any of you know how to style this? I already have the CSS done for it, but whenever I try it, it doesn't work correctly.
And, would you know how to get the description of the photo using the API?
Thank you in advance :-)
You need to use Instagram's count= url parameter when requesting data from their endpoints.
For example: https://api.instagram.com/v1/users/search?count=6
Or in your code:
<?PHP
$token = 'token';
$username = 'username';
$userInfo = json_decode(file_get_contents('https://api.instagram.com/v1/users/search?count=6&q='.$username.'&access_token='.$token));
if($userInfo->meta->code==200){
$photoData = json_decode(file_get_contents('https://api.instagram.com/v1/users/'.$userInfo->data[0]->id.'/media/recent/?count=6&access_token='.$token));
if($photoData->meta->code==200){ ?>
<?PHP foreach($photoData->data as $img){
echo '<img src="'.$img->images->thumbnail->url.'">';
} ?>
<?PHP } // If
} // If
?>
Pseudo example for styling. You'll need to figure out the css styles for that, but shouldn't be to difficult.
<div class='myBorder'>
<img url=$img->link />
<div class='myCaption'>$img->caption->text</div>
</div>
To get the description
if (isset($img->caption)) {
if (get_magic_quotes_gpc()) {
$title = stripslashes($img->caption->text);
} else {
$title = $img->caption->text;
}
}

PHP how to get facebook profile name

function getTitle($Url)
{
$str = file_get_contents($Url);
if(strlen($str)>0)
{
preg_match("/\<title(.*)\<\/title\>/",$str,$title);
if(empty($title))
{
$dom = new DOMDocument();
#$dom->loadHTML($str);
$title = $dom->getElementsByTagName('title');
if(empty($title->item(0)->nodeValue))
return "";
else
return $title->item(0)->nodeValue;
}
else
return $title[1];
}
}
I used two ways to get the title tags of facebook but it not working. The facebook site reading info from my site host but I want it to read from user's browser
I need any way to read the title tags
<title> ..... </title>
of the facebook site. If I logged in the facebook site the title tags will be
<title id="pageTitle">Facebook</title>
PHP is server side programming language, so you can not read from user browser directly. For server side your function may work or try this one that it been tested for you :
$myURL = 'http://www.google.com';
if (preg_match(
'/<title>(.+)<\/title>/',
file_get_contents($myURL),$matches)
&& isset($matches[1] )
$title = $matches[1];
else
$title = "Not Found";

How to get 5 day Yahoo weather forecast

I've been working on a weather feed for my website.
I'm currently only able to get forecasts for the next 2 days. I want forecasts for the next 5 days.
Here's my code:
$ipaddress = $_SERVER['REMOTE_ADDR'];
$locationstr = "http://api.locatorhq.com/?user=MYAPIUSER&key=MYAPIKEY&ip=".$ipaddress."&format=xml";
$xml = simplexml_load_file($locationstr);
$city = $xml->city;
switch ($city)
{
case "Pretoria":
$loccode = "SFXX0044";
$weatherfeed = file_get_contents("http://weather.yahooapis.com/forecastrss?p=".$loccode."&u=c");
if (!$weatherfeed) die("weather check failed, check feed URL");
$weather = simplexml_load_string($weatherfeed);
readWeather($loccode);
break;
}
function readWeather($loccode)
{
$doc = new DOMDocument();
$doc->load("http://weather.yahooapis.com/forecastrss?p=".$loccode."&u=c");
$channel = $doc->getElementsByTagName("channel");
$arr;
foreach($channel as $ch)
{
$item = $ch->getElementsByTagName("item");
foreach($item as $rcvd)
{
$desc = $rcvd->getElementsByTagName("description");
$_SESSION["weather"] = $desc->item(0)->nodeValue;
}
}
}
I'd like to direct your attention to the lines that query for the weather:
$doc = new DOMDocument();
$doc->load("http://weather.yahooapis.com/forecastrss?p=".$loccode."&u=c");
// url resolves to http://weather.yahooapis.com/forecastrss?p=SFXX0044&u=c in this case
Searching google, I found this link which suggested I use this url instead:
$doc->load("http://xml.weather.yahoo.com/forecastrss/SFXX0044_c.xml");
While this also works and I see a 5 day forecast in the XML file, I still only see 2 days forecast on my site.
I have a feeling this is because I'm leveraging the channel child element found in the RSS feed, while the XML feed has no such child element.
If anyone can provide any insight here, I would really appreciate it.
This is what I get for asking questions too early...
As I was looking over my code again, I noticed that I had the yahooapis URL referenced twice: once in the switch, and again in readWeather.
Having removed the redundant reference and updating the url as per the thread mentioned, I see that it does work now.
See updated code for reference:
switch ($city)
{
case "Pretoria":
$loccode = "SFXX0044";
readWeather($loccode);
break;
}
function readWeather($loccode)
{
$doc = new DOMDocument();
$doc->load("http://xml.weather.yahoo.com/forecastrss/".$loccode."_c.xml");
$channel = $doc->getElementsByTagName("channel");
$arr;
foreach($channel as $ch)
{
$item = $ch->getElementsByTagName("item");
foreach($item as $rcvd)
{
$desc = $rcvd->getElementsByTagName("description");
$_SESSION["weather"] = $desc->item(0)->nodeValue;
}
}
}

How to display RSS feeds from other sites

I have been researching this topic for a few days now and i'm still non the wiser as on how to do it.
I want to get an RSS feed from forexfactory.com to my website, i want to do some formatting on whats happening and i also want the latest information from them (Although those last two points can wait as long as i have some more or feed running).
Preferably I'd like to develop this from the ground up if anyone knows of a tutorial or something i could use?
If not i will settle for using a third party API or something like that as long as i get to do some of the work.
I'm not sure what it is but there is something about RSS that i'm not getting so if anyone knows of any good, probably basic tutorials that would help me out a lot. It's kind of hard going through page after page of google searches.
Also i'm not to fussed on the language it's outputted in Javascript, PHP or HTML will be great though.
Thanks for the help.
It looks like SimplePie may be what you are looking for. It's a very basic RSS plugin which is quite easy to use and is customisable too. You can download it from the website.
You can use it at it's bare bones or you can delve deeper in to the plugin if you wish. Here's a demo on their website.
index.php
include('rss_class.php');
$feedlist = new rss($feed_url);
echo $feedlist->display(2,"Feed Title");
rss_class.php
<?php
class rss {
var $feed;
function rss($feed){
$this->feed = $feed;
}
function parse(){
$rss = simplexml_load_file($this->feed);
//print_r($rss);die; /// Check here for attributes
$rss_split = array();
foreach ($rss->channel->item as $item) {
$title = (string) $item->title;
$link = (string) $item->link;
$pubDate = (string) $item->pubDate;
$description = (string) $item->description;
$image = $rss->channel->item->enclosure->attributes();
$image_url = $image['url'];
$rss_split[] = '
<li>
<h5>'.$title.'</h5>
<span class="dateWrap">'.$pubDate.'</span>
<p>'.$description.'</p>
Read Full Story
</li>
';
}
return $rss_split;
}
function display($numrows,$head){
$rss_split = $this->parse();
$i = 0;
$rss_data = '<h2>'.$head.'</h2><ul class="newsBlock">';
while($i<$numrows){
$rss_data .= $rss_split[$i];
$i++;
}
$trim = str_replace('', '',$this->feed);
$user = str_replace('&lang=en-us&format=rss_200','',$trim);
$rss_data.='</ul>';
return $rss_data;
}
}
?>
I didn't incorporate the < TABLE > tags as there might be more than one article that you would like to display.
class RssFeed
{
public $rss = "";
public function __construct($article)
{
$this->rss = simplexml_load_file($article, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING);
if($this->rss != false)
{
printf("<TR>\r\n");
printf("<TD>\r\n");
printf("<h3>%s</h3>\r\n", $this->rss->channel->title);
printf("</TD></TR>\r\n");
foreach($this->rss->channel->item as $value)
{
printf("<TR>\r\n");
printf("<TD id=\"feedmiddletd\">\r\n");
printf("<A target=\"_blank\" HREF=\"%s\">%s</A><BR/>\r\n", $value->link, $value->title);
printf($value->description);
printf("</TD></TR>\r\n");
}
}
}
}

Categories