I am trying to get the post link of a RSS feed. I load all the posts in an array correctly ( I successfully echo the content and other tags) but I have a problem to get the link.
In the feed, the link can be found by two ways
1.
<link rel="alternate" type="text/html" href="this is the address I want" title="here goes the title" />
and tried <?php echo $post->link[href]; ?> but because there are a lot of link tags in a content, it must echo the one that has rel="alternate"
2.
<feedburner:origLink>this is the address</feedburner:origLink>
and tried <?php echo $post->feedburner:origLink; ?>
My question is how to get the link ? I prefer the 2nd way because it does not go through the feedburner link.
Note: I use two RSS XML structures in the array so what I will use is something like this
($post->description)?$post->description:$post->content) as I do for the description/content
1. rel=alternate
$links = $post->xpath('link[#rel="alternate" and #type="text/html"]');
$link = (string) $links[0]['href'];
See http://php.net/simplexmlelement.xpath and http://php.net/simplexml.examples-basic (Example #5)
2. feedburner:origLink
$links = $post->xpath('feedburner:origLink');
$link = (string) $links[0];
// or
$link = (string) $post->children('feedburner', TRUE)->origLink;
See http://php.net/simplexmlelement.children
I had the same problem but I solved it with the follow:
$link = $xml->entry[$i]->link[2]->attributes()->href;
//the feed-blog has 3 type of links
where probably $xml is $post for you.
Related
I'm trying to get Facebook's meta tags from my HTML.
I'm using simple html dom to get all html data from the site.
I've tried with preg_replace, but without luck.
I want for example to get the content of this fb meta tag:
<meta content="IMAGE URL" property="og:image" />
Hope someone can help! :-)
I Was going to suggest to use get_meta_tags() but it seems to not work (for me) :s
<?php
$tags = get_meta_tags('http://www.example.com/');
echo $tags['og:image'];
?>
But I would rather suggest using DOMDocument anyways:
<?php
$sites_html = file_get_contents('http://example.com');
$html = new DOMDocument();
#$html->loadHTML($sites_html);
$meta_og_img = null;
//Get all meta tags and loop through them.
foreach($html->getElementsByTagName('meta') as $meta) {
//If the property attribute of the meta tag is og:image
if($meta->getAttribute('property')=='og:image'){
//Assign the value from content attribute to $meta_og_img
$meta_og_img = $meta->getAttribute('content');
}
}
echo $meta_og_img;
?>
Hope it helps
As per this method you will get key pair array of fabcebook open graph tags.
$url="http://fbcpictures.in";
$site_html= file_get_contents($url);
$matches=null;
preg_match_all('~<\s*meta\s+property="(og:[^"]+)"\s+content="([^"]*)~i', $site_html,$matches);
$ogtags=array();
for($i=0;$i<count($matches[1]);$i++)
{
$ogtags[$matches[1][$i]]=$matches[2][$i];
}
This is my code :
$patt = "#href=\"(.*?)\"#";
preg_match($patt,$data,$match);
echo $match[1];`
i.e. theres a URL in the HTML code of the page $data
<a href="http://aba.ai/iEU9x">
I want to grab this link above. Thanks
I'm working on a rss feed for a website I made. It takes input from my home made news function on the site, which is stored in a MySQL database.
Now I can get the text nicely enough, but when I try to use <enclosure> to put in an image, nothing shows up.
The code i use to insert the code is as follows:
if($rows['image'] != 0) {
$image = mysql_fetch_array(mysql_query("SELECT * FROM dafl_news_imagedb WHERE id = '".$rows['image']."' LIMIT 1"));
$imageUrl = "http://dafl.dk/content/news/pics/".$image['filename'];
$imageType = substr($imageUrl, strlen($imageUrl) - 3, 3);
$enclosedImage = '
<enclosure url="'.$imageUrl.'" length="0" type="image/'.$imageType.'" />
';
echo $enclosedImage;
}
and in the source code of the rss:
<enclosure url="http://dafl.dk/content/news/pics/13.png" length="0" type="image/png" />
The link to the rss is:
http://dafl.dk/rss/?language=en
(The picture is only included when an image is present for the newspost. Is this a problem - that not all items have an enclosure ?
Try this link to see if the enclosure tag works in the target browser.
http://www.w3schools.com/rss/tryrss.asp?filename=rss_ex_enclosure
References:
http://www.w3schools.com/rss/rss_tag_enclosure.asp
I'm trying to get Facebook's meta tags from my HTML.
I'm using simple html dom to get all html data from the site.
I've tried with preg_replace, but without luck.
I want for example to get the content of this fb meta tag:
<meta content="IMAGE URL" property="og:image" />
Hope someone can help! :-)
I Was going to suggest to use get_meta_tags() but it seems to not work (for me) :s
<?php
$tags = get_meta_tags('http://www.example.com/');
echo $tags['og:image'];
?>
But I would rather suggest using DOMDocument anyways:
<?php
$sites_html = file_get_contents('http://example.com');
$html = new DOMDocument();
#$html->loadHTML($sites_html);
$meta_og_img = null;
//Get all meta tags and loop through them.
foreach($html->getElementsByTagName('meta') as $meta) {
//If the property attribute of the meta tag is og:image
if($meta->getAttribute('property')=='og:image'){
//Assign the value from content attribute to $meta_og_img
$meta_og_img = $meta->getAttribute('content');
}
}
echo $meta_og_img;
?>
Hope it helps
As per this method you will get key pair array of fabcebook open graph tags.
$url="http://fbcpictures.in";
$site_html= file_get_contents($url);
$matches=null;
preg_match_all('~<\s*meta\s+property="(og:[^"]+)"\s+content="([^"]*)~i', $site_html,$matches);
$ogtags=array();
for($i=0;$i<count($matches[1]);$i++)
{
$ogtags[$matches[1][$i]]=$matches[2][$i];
}
I want to replace in post and archive custom field the youtube url with php.
From: http://www.youtube.com/watch?v=FKAjQfL31Dw To:
http://www.youtube.com/v/FKAjQfL31Dw
My code is:
<?php if ( get_post_meta($post->ID, 'ixosrip', true) ) { ?>
<embed style="width:150px;height:25px;" allowfullscreen="false" type="application/x-shockwave-flash" src="http://www.youtube.com/v/<?php echo get_post_meta($post->ID, "ixosrip", $single = true); ?>&ap=%2526fmt%3D18&autoplay=0&rel=0&fs=1&color1=0xC0C0C0&color2=0xFFFFFF&border=0&loop=0">
<?php } else { ?>
<em>No sound</em>
<?php } ?>
Current i use a javascript, taked from here: find all youtube links with js (jquery) / But its loads very slow my site.
Is there any way to do this with php in the custom field?
Thank you.
David.
If you can iterate over your custom fields, just do it with a simple PHP str_replace:
$new_url = str_replace('/watch?v=', '/v/', $old_url);
Where $old_url is your video's current slug.
UPDATED ANSWER: You could parse the URL, grabbing the v component:
parse_str($video_url, $params);
$video_id = $params['v'];
$video_url = 'http://www.youtube.com/v/'.$video_id;
You would put this in your loop or your post page template or whatever, where $video_url is the field containing, well, the YouTube video URL you want to alter.
Aye there,
One thing I can think of is making 2 custom fields where one would be the id and would have a value like 'FKAjQfL31Dw'
...and the other would have a value of either 'v/' or 'watch?v=' depending on which one u choose. (can be done via radio buttons)
so ur code might look something like
<embed style="width:150px;height:25px;" allowfullscreen="false" type="application/x-shockwave-flash" src="http://www.youtube.com/
<?php $key='youtube_format'; echo get_post_meta($post->ID, $key, true); ?>
<?php $key='youtube_id'; echo get_post_meta($post->ID, $key, true); ?>
&ap=%2526fmt%3D18&autoplay=0&rel=0&fs=1&color1=0xC0C0C0&color2=0xFFFFFF&border=0&loop=0">
If your trying to do this in archives, you would most likely add this code to content.php or content-video.php.