PHP how to get facebook profile name - php

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";

Related

Php remove html tags when reading a page

I have a script to read Shoutcast live broadcast informations.
I am getting name of track and searching it on spotify api to get cover art.
My code:
$sc_url_ip = "streamip";
$sc_url_port = "streamport";
function getNowPlaying($sc_url_ip,$sc_url_port)
{
$open = fsockopen($sc_url_ip,$sc_url_port,$errno,$errstr,'.5');
if ($open) {
fputs($open,"GET /7.html HTTP/1.1\nUser-Agent:Mozilla\n\n");
stream_set_timeout($open,'1');
$read = fread($open,200);
$text = explode(",",$read);
if($text[6] == '' || $text[6] == '</body></html>'){
$msg = ' live stream offline'; }
else {
$msg = $text[6];
}
$text = $msg;
}
else {
return false;
}
fclose($open);
return $text;
}
$current_song = getNowPlaying($sc_url_ip,$sc_url_port);
echo $current_song;
But when searching with spotify api, my track name look like this in source code:
Pink Floyd - Welcome to the Machine</body></html>
Because of html tags, spotify api search results zero. See search url:
https://api.spotify.com/v1/search?query=Pink+Floyd+-+Welcome+to+the+Machine%3C%2Fbody%3E%3C%2Fhtml%3E%3B&type=track
I tried strip_tags but I couldnt.
How can I remove these </body></html> tags from $current_song ?
The variable $current_song contains the html tag right? Then to remove it, try this:
$current_song = strip_tags(getNowPlaying($sc_url_ip,$sc_url_port));

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?

rewrite/mask external link inside article

I want to rewrite/mask all external url in my article and also add nofollow and target="_blank". So that original link to the external site is get encrypted/ masked/ rewritten.
For example:
original link: www.google.com
rewrite it to: www.mydomain.com?goto=google.com
There is a plugin for joomla which rewrite external link: rewrite plugin.
But I am not using joomla. Please have a look at above plugin, It does exactly what I am looking for.
What I want?
$article = "hello this is example article I want to replace all external link http://google.com";
$host = substr($_SERVER['HTTP_HOST'], 0, 4) == 'www.' ? substr($_SERVER['HTTP_HOST'], 0) : $_SERVER['HTTP_HOST'];
if (thisIsNotMyWebsite){
replace external url
}
You can use DOMDocument to parse and traverse the document.
function rewriteExternal($html) {
// The url for your redirection script
$prefix = 'http://www.example.com?goto=';
// a regular expression to determine if
// this link is within your site, edit for your
// domain or other needs
$is_internal = '/(?:^\/|^\.\.\/)|example\.com/';
$dom = new DOMDocument();
// Parse the HTML into a DOM
$dom->loadHTML($html);
$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
$href = $link->getAttribute('href');
if (!preg_match($is_internal, $href)) {
$link->getAttributeNode('href')->value = $prefix . $href;
$link->setAttributeNode(new DOMAttr('rel', 'nofollow'));
$link->setAttributeNode(new DOMAttr('target', '_blank'));
}
}
// returns the updated HTML or false if there was an error
return $dom->saveHTML();
}
This approach will be much more reliable than using a regular expression based solution since it actually parses the DOM for you instead of relying on a often-fragile regex.
something like:
<?php
$html ='1224 google 567';
$tracking_string = 'http://example.com/track.php?url=';
$html = preg_replace('#(<a[^>]+href=")(http|https)([^>" ]+)("?[^>]*>)#is','\\1'.$tracking_string.'\\2\\3\\4',$html);
echo $html;
in action here: http://codepad.viper-7.com/7BYkoc
--my last update
<?php
$html =' 1224 google 567';
$tracking_string = 'http://example.com/track.php?url=';
$html = preg_replace('#(<a[^>]+)(href=")(http|https)([^>" ]+)("?[^>]*>)#is','\\1 nofollow target="_blank" \\2'.$tracking_string.'\\3\\4\\5',$html);
echo $html;
http://codepad.viper-7.com/JP8sUk

PHP SimpleXML Breaking when trying to traverse nodes

I'm trying to read the xml information that tumblr provides to create a kind of news feed off the tumblr, but I'm very stuck.
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if (!$xml)
{
exit('Failed to retrieve data.');
}
else
{
foreach ($xml->posts[0] AS $post)
{
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo .$title.;
echo '<p>'.$small_post.'</p>';
}
}
?>
Which always breaks as soon as it tries to go through the nodes. So basically "tumblr->posts;....ect" is displayed on my html page.
I've tried saving the information as a local xml file. I've tried using different ways to create the simplexml object, like loading it as a string (probably a silly idea). I double checked that my webhosting was running PHP5. So basically, I'm stuck on why this wouldn't be working.
EDIT: Ok I tried changing from where I started (back to the original way it was, starting from tumblr was just another (actually silly) way to try to fix it. It still breaks right after the first ->, so displays "posts[0] AS $post....ect" on screen.
This is the first thing I've ever done in PHP so there might be something obvious that I should have set up beforehand or something. I don't know and couldn't find anything like that though.
This should work :
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if ( !$xml ){
exit('Failed to retrieve data.');
}else{
foreach ( $xml->posts[0] AS $post){
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo $title;
echo '<p>'.$small_post.'</p>';
echo '<hr>';
}
}
First thing in you code is that you used root element that should not be used.
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if (!$xml)
{
exit('Failed to retrieve data.');
}
else
{
foreach ($xml->posts->post as $post)
{
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo .$title.;
echo '<p>'.$small_post.'</p>';
}
}
?>
$xml->posts returns you the posts nodes, so if you want to iterate the post nodes you should try $xml->posts->post, which gives you the ability to iterate through the post nodes inside the first posts node.
Also as Needhi pointed out you shouldn't pass through the root node (tumblr), because $xml represents itself the root node. (So I fixed my answer).

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