Based on the thread With PHP preg_match_all, get value of href, I'm trying to get some information from a twitter feed.
Here is the feed url (for testing purpose): Twitter feed
Here is my code:
function parse_feed($process) {
$xml = #simplexml_load_string($process);
$findTweet = $xml['entry'];
return $findTweet;
}
$username = 'tweet';
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=2";
$feed = file_get_contents($feed);
//echo $feed;
print_r(parse_feed($feed));
I never used SimpleXML before or worked with XML.
Can someone help me please?
this site might be a good start http://www.php.net/manual/en/simplexml.examples-basic.php
Okay Founf it!, here is the solution for who is interested...
Thanks to m1k3y02 for the docs!
function parse_feed($process) {
$xml = new SimpleXMLElement($process);
$n=0;
foreach($xml->entry as $entry) {
$tweets[$n] = array($entry->published,$entry->content);
$n++;
}
return $tweets;
}
$twitter_username = 'tweet';
$twitter_entries = 5;
$feed = "http://search.twitter.com/search.atom?q=from:" . $twitter_username . "&rpp=".$twitter_entries;
$feed = file_get_contents($feed);
$tweets = parse_feed($feed);
$n=0;
$n_t = count($tweets);
while($n < $n_t) {
echo "<div class=\"tweet\"><img src=\"img/tweet.png\" valign=\"absmiddle\" /> ";
echo $tweets[$n][1][0];
echo "</div>";
echo "<div class=\"date\">".$tweets[$n][0][0]."</div>";
$n++;
}
Related
I have a Facebook API generated feed on my website that shows the last post of whatever I posted on Facebook. In this case, it was an article I wrote about Launtua Island. When I visit my Facebook page, the image looks like this:
But when I visited my website, the image looks like this:
It appears Facebook is cropping my image, but I don't know how to prevent it. This is the code I'm using:
$feed = 'Feed Id';
$a_token = 'Secret Access Token';
$maximum = 1;
$url = "https://graph.facebook.com/v2.9/{$feed}/feed?access_token={$a_token}&limit={$maximum}";
$response = file_get_contents($url);
$facebookString = $response;
$facebookArray = json_decode($facebookString);
$array = array();
foreach ( $facebookArray->data as $each ) {
$each->message = preg_replace("/(https?:\/\/.*) ?$/", "<a target='_blank' href='$1'>$1</a>", $each->message);
$row = array();
$response = file_get_contents("https://graph.facebook.com/v2.9/{$each->id}/attachments?access_token={$a_token}");
$image = json_decode($response);
$src = $image->data[0]->media->image->src;
if ($src == '') {
$src = $image->data[0]->subattachments->data[0]->media->image->src;
}
$row['post'] = $each->message;
$row['image'] = $src;
$row['time'] = strtotime($each->created_time);
$array[] = $row;
}
$string = "";
foreach ($array as $entry) {
$string .= "<div class='post'>";
if ($entry['image'] != '') {
$string .= "<img src='{$entry['image']}'>";
}
$string .= "<p>" . $entry['post'] . "</p>";
$string .= "</div>";
}
echo $string;
There is a lot of similar posts to my problem, but I'm not sure this is the same issue (not asking for image when making request) or a different one.
It would be great if Facebook stopped changing their API all the time!
Any help would be appreciate.
Thank you.
how can I get the content-Element from XML? Other solutions on stackoverflow could not help me, I'm doing something wrong.. This is how I read the feed:
<?php
$content = file_get_contents('some feed url');
$x = new SimpleXmlElement($content);
foreach($x->channel->item as $entry) {
$chi = $entry->children('content', true)->encoded;
}
?>
XML: https://pastebin.com/79dgx8cU
Try following code:
$x = new SimpleXmlElement($str);
$x->registerXPathNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
$result = $x->xpath('//channel/item/content:encoded');
if(count($result) > 0) {
foreach($result as $element) {
echo (string)$element . "\n";
}
}
I get
main(): Node no longer exists
while trying to display images (media:content)from a Rss url. I have been trying to solve it but was not successful . Can someone explain me Why this error is displaying and Help me solve it.the code works well if I Parsed RSS Feeds Urls with a .xml extension but not with a .cms Extension Urls.I Dont know why this is happening or whats wrong with my CODE.
My View
foreach ($data1 as $key1 => $value1) {
$image=$data1[$key1]['image']->children('http://search.yahoo.com/mrss/')->content->attributes()->url;
echo '<p>'.$image.'</p>';
echo '<div class="col-lg-6">';
echo '<img src="'.$image.'" /><strong style="color:pink;">'.$data1[$key1]["title"].'</strong>';
echo '<p>'.$data1[$key1]["description"].'</p>';
echo '<p>'.$data1[$key1]["pubDate"].'</p>';
echo '</div>';
}
My Controller
foreach($urls as $key => $url){
$xml = simplexml_load_file($url);
$result[$key]['title'] = $xml->channel->title;
$data = [];
for ($i=0; $i<5 ; $i++) {
# code...
$items = [];
if($xml->channel->item[$i] == null)
{
break;
}
$items['title'] = $xml->channel->item[$i]->title;
$items['link'] = $xml->channel->item[$i]->link;
$items['description'] = $xml->channel->item[$i]->description;
$items['pubDate'] = $xml->channel->item[$i]->pubDate;
$items['image'] = $xml->channel->item[$i];
$data[$i] = $items;
}
$result[$key]['data'] = $data;
//$entries = array_merge($worldFeed,$entries);
}
return View::make('index')->with('result',$result);
Please help me get the content of page like:
http://www.youtube.com/my_videos_annotate?feature=vm&v=someVideoId
using PHP/Curl.
I think I need first log into YouTube's service, but don't know how to do this.
Use youtube-api
Example
$feedURL = 'http://gdata.youtube.com/feeds/api/users/**ACCOUNTNAME**/uploads?max-results=50';
$sxml = simplexml_load_file($feedURL);
$i=0;
foreach ($sxml->entry as $entry) {
$media = $entry->children('media', true);
$watch = (string)$media->group->player->attributes()->url;
$thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;
$input = array('http://www.youtube.com/watch?v=','&feature=youtube_gdata_player');
$change = array('','') ;
$link = #str_replace ($input ,$change, $watch);
echo $link.'</br>';
echo $thumbnail.'</br>'; //**thumbnail
echo $media->group->title.'</br>'; //**title
echo $media->group->description.'</br>'; //**description
}
Hope This will help
Here is the URL of the xml source:
I'm tryng to grab all the RichText elements using xpath relative location and then print the elementID attribute. It is outputting nothing though. Any ideas?
<?php
$url = "FXG";
$xml = simplexml_load_file($url);
//print_r($xml);
$textNode = $xml->xpath("//RichText");
$count = count($textNode);
$i = 0;
while($i < $count)
{
echo '<h1>'.$textNode[$i]['s7:elementID'].'</h1>';
$i++;
}
?>
You need to register the namespaces that are set in the xml
$url = "http://testvipd7.scene7.com/is/agm/papermusepress/HOL_12_F_green?&fmt=fxgraw";
$xml = simplexml_load_file($url);
$xml->registerXPathNamespace('default', 'http://ns.adobe.com/fxg/2008');
$xml->registerXPathNamespace('s7', 'http://ns.adobe.com/S7FXG/2008');
$textNode = $xml->xpath("//default:RichText/#s7:elementID");
foreach($textNode as $node) {
echo '<h1>'.$node[elementID].'</h1>';
}
I hope this helps.
Strange. This, however, works.
$textNode = $xml->xpath("//*[name() = 'RichText']");