Print out favicon instead of link to it - php

I'm trying to print a website's favicon, as an image, not as a link to it.
I have a php script in which I extract the favicon, but now I want to show it as it is.
Here is what I've tried.
//extract favicon
$url = $_POST['url'];
$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
$doc->loadHTML(file_get_contents($url));
$xml = simplexml_import_dom($doc);
$arr = $xml->xpath('//link[#rel="shortcut icon"]');
echo "<br>";
//echo "favicon:";
if( $arr)
{
$src = $arr[0]['href'];
echo "<img src = "$src">";//as I can see, the parameter here cannot be a variable
//second thing that I've tried: echo "<img src = "$arr[0]['href']""; it doesn't work either
}
This is what my script is echoing right now. http://i.stack.imgur.com/Wkoyj.jpg
Instead of the link to the favicon, I want the actual favicon to be displayed. I hope I explained myself correctly.

Your error is with the code:
echo "<img src = "$src">";//as I can see, the parameter here cannot be a variable
It should be
echo '<img src="'.$src.'">';
Or even
echo "<img src=\"$src\">";

Related

Display rss xml index in html

I am adding an RSS feed to my website. I created the RSS.xml index file and next I want to display its contents in a nicely formatted way in a webpage.
Using PHP, I can do this:
$index = file_get_contents ($path . 'RSS.xml');
echo $index;
But all that does is dump the contents as a long stream of text with the tags removed.
I know that treating RSS.xml as a link, like this:
<a href="../blogs/RSS.xml">
<img src="../blogs/feed-icon-16.gif">Blog Index
</a>
causes my browser to parse and display it in a reasonable way when the user clicks on the link. However I want to embed it directly in the web page and not make the user go through another click.
What is the proper way to do what I want?
Use the following code:
include_once('Simple/autoloader.php');
$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->enable_cache(false);
$feed->set_output_encoding('utf-8');
$feed->init();
$i=0;
$items = $feed->get_items();
foreach ($items as $item) {
$i++;
/*You are getting title,description,date of your rss by the following code*/
$title = $item->get_title();
$url = $item->get_permalink();
$desc = $item->get_description();
$date = $item->get_date();
}
Download the Simple folder data from : https://github.com/jewelhuq/Online-News-Grabber/tree/master/worldnews/Simple
Hope it will work for you. There $url mean your rss feed url. If you works then response.
Turns out, it's simple by using the PHP xml parer function:
$xml = simplexml_load_file ($path . 'RSS.xml');
$channel = $xml->channel;
$channel_title = $channel->title;
$channel_description = $channel->description;
echo "<h1>$channel_title</h1>";
echo "<h2>$channel_description</h2>";
foreach ($channel->item as $item)
{
$title = $item->title;
$link = $item->link;
$descr = $item->description;
echo "<h3><a href='$link'>$title</a></h3>";
echo "<p>$descr</p>";
}

Filter out link address from DOM results

I'm working with a DOM parser that grabs links from a website by the class thumbnail. This returns a list of links. They are then converted to their image state and shown on the page. The problem I'm having is I have 2 different links that are getting returned:
http://i.imgur.com/randomstuffhere
AND
http://imgur.com/randomstuffhere
I need to filter the results for the links that DO NOT contain the i.imgur.com. If the link is a imgur link but does not contain the i. before I need to filter it out not to show.
I have this so far and I cannot figure out where I've gone wrong... Any suggestions?
<?php
$html = file_get_contents('http://www.reddit.com/r/funny');
$dom = new DOMDocument();
#$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$hyperlinks = $xpath->evaluate('//a[#class="thumbnail "]');
foreach($hyperlinks as $hyperlink) {
if (preg_match("/http://imgur.com/", $hyperlink->getAttribute('href'))){
}
else{
echo "<img style='padding-left:30%' width=\"500\" src=\"" . $hyperlink->getAttribute('href') . "\" alt=\"\" />";
echo "<br />";
}
}
?>
You need to escape the // in http:// with \/\/.
You should probably use strpos, though.
if(strpos($hyperlink->getAttribute('href'), 'http://i.imgur.com/') !== FALSE){
echo "This is an i.imgur.com link!";
}

PHP Image not showing in HTML using img element

Hello there i have a php file with the included:
The image shows properly when i access the PHP file, however when I try to show it in the HTML template, it shows as the little img with a crack in it, so basically saying "image not found"
<img src="http://konvictgaming.com/status.php?channel=blindsniper47">
is what i'm using to display it in the HTML template, however it just doesn't seem to want to show, I've tried searching with next to no results for my specific issue, although I'm certain I've probably searched the wrong title
adding code from the OP below
$clientId = ''; // Register your application and get a client ID at http://www.twitch.tv/settings?section=applications
$online = 'online.png'; // Set online image here
$offline = 'offline.png'; // Set offline image here
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($channelName).'?client_id='.$clientId), true);
if ($json_array['stream'] != NULL) {
$channelTitle = $json_array['stream']['channel']['display_name'];
$streamTitle = $json_array['stream']['channel']['status'];
$currentGame = $json_array['stream']['channel']['game'];
echo "<img src='$online' />";
} else {
echo "<img src='$offline' />";
}
The url is not an image, it is a webpage with the following content
<img src='offline.png' alt='Offline' />
Webpages cannot be displayed as images. You will need to edit the page to only transmit the actual image, with the correct http-headers.
You can probably find some help on this by googling for "php dynamic image".
Specify in the HTTP header that it's a PNG (or whatever) image!
(By default they are interpreted as text/html)
in your status.php file, where you output the markup of <img src=... change it to read as follows
$image = file_get_contents("offline.png");
header("Content-Type: image/png");
echo $image;
Which will send an actual image for the request instead of sending markup. markup is not valid src for an img tag.
UPDATE your code modified below.
$clientId = ''; // Register your application and get a client ID at http://www.twitch.tv/settings?section=applications
$online = 'online.png'; // Set online image here
$offline = 'offline.png'; // Set offline image here
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($channelName).'?client_id='.$clientId), true);
header("Content-Type: image/png");
$image = null;
if ($json_array['stream'] != NULL) {
$channelTitle = $json_array['stream']['channel']['display_name'];
$streamTitle = $json_array['stream']['channel']['status'];
$currentGame = $json_array['stream']['channel']['game'];
$image = file_get_contents($online);
} else {
$image = file_get_contents($offline);
}
echo $image;
I suppose you change the picture dynmaclly on this page.
Easiest way with least changes will just be using an iframe:
<iframe src="http://konvictgaming.com/status.php?channel=blindsniper47"> </iframe>

Get image source from html dom element

I am querying image using getElementsByTagName("img") and printing it using image->src , it does not work. I also tried to use image->nodeValue this to does not work.
require('simple_html_dom.php');
$dom=new DOMDocument();
$dom->loadHTML( $str); /*$str contains html output */
$xpath=new DOMXPath($dom);
$imgfind=$dom->getElementsByTagName('img'); /*finding elements by tag name img*/
foreach($imgfind as $im)
{
echo $im->src; /*this doesnt work */
/*echo $im->nodeValue; and also this doesnt work (i tried both of them separately ,Neither of them worked)*/
// echo "<img src=".$im->nodeValue."</img><br>"; //This also did not work
}
/*the image is encolsed within div tags.so i tried to query value of div and print but still image was not printed*/
$printimage=$xpath->query('//div[#class="abc"]');
foreach($printimage as $image)
{
echo $image->src; //still i could not accomplish my task
}
Okay, use this to display your image:
foreach($imgfind as $im)
{
echo "<img src=".$im->getAttribute('src')."/>"; //use this instead of echo $im->src;
}
and it will surely display your image. Make sure path to the image is correct.
Espero te sirva
$dom = new DOMDocument();
$filename = "https://www.amazon.com/dp/B0896WB9XD/";
$html = file_get_contents($filename);
#$dom->loadHTML($html);
$imgfind=$dom->getElementsByTagName('img');
foreach($imgfind as $im)
{
$ids= $im->getAttribute('id');
if ($ids == 'landingImage') {
$im2 = $im->getAttribute('src');
echo '<img src="'.$im2.'">';
}
else{
}
}
para amazon.

How to retrieve an attribute using getElementsByTagNameNS in php?

I want to pull the news feed from "http://rapfix.mtv.com/feed" for a website that I'm creating. I have everything working other than, being able to pull the URL location of the image for each article.
In this feed, the image URL is showing up like this in the code:
<media:content url="http://rapfix.mtv.com/wp-content/uploads/2011/05/tyler-handcuff.jpg" type="image/jpeg" height="300" width="575">
<media:text type="plain"><![CDATA[tyler-handcuff]]></media:text>
</media:content>
I've read from another stackoverflow question, that you're able to pull information from the node using something like this:
$item_pic = $article->getElementsByTagNameNS('http://purl.org/rss/1.0/modules/content/', 'content')->item(0);
But now, I'm trying to get the "URL" attribute out of it. Here's a look of my code:
$xml=("http://rapfix.mtv.com/feed");
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$x = $xmlDoc->getElementsByTagName('item');
foreach($x as $article){
$item_title = $article->getElementsByTagName('title')->item(0)->nodeValue;
$item_link = $article->getElementsByTagName('link')->item(0)->nodeValue;
$item_desc = $article->getElementsByTagName('description')->item(0)->nodeValue;
$item_pic = $article->getElementsByTagNameNS('http://purl.org/rss/1.0/modules/content/', 'content')->item(0);
echo ("<strong><a href='".$item_link."' target='_blank'>".$item_title."</a></strong><br />");
echo ("<div><div class='FloatLeft'><img src='".$item_pic."' width='100' height='100'/></div><div class='FloatLeft'>".$item_desc." - <a href='".$item_link."' target='_blank'>Read More</a></div>^");
}
Any ideas on how to get this done?
The namespace for your target element is media. The element name is content. The Namespace URL for the media namespace is http://search.yahoo.com/mrss/. Thus:
foreach($x as $article)
{
$nlContent = $article->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'content');
if( $nlContent->length > 0 )
$item_pic = $nlContent->item(0)->getAttribute('url');
else
$item_pic = '/images/noimageavailable.jpg';
echo $item_pic . "\n";
}

Categories