I'm using the following code to read a RSS feed. The problem is that I get wrong encoding. The code is in a file with UTF-8 encoding. Is there anything else I have to do to get it right?
$feed_url = "http://lujanenlinea.com.ar/noticias/feed";
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<div class='rss-container'>";
echo "<ul class='rss-content'>";
foreach($x->channel->item as $entry) {
echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
}
echo "</ul>";
Maybe use utf8_encode() or utf8_decode()
Related
How can I go about convering a public instagram URL into JSON using PHP? Ex: https://www.instagram.com/explore/tags/brindle/
I can't use the API as I need public hashtag content and my use case won't qualify for their app review process :-(.
Here is what I have so far but it does not pull all images. Also, I'd like to be able to load the "load more" images as well. Any help would be much appreciated!
$instagram_source = file_get_contents("https://www.instagram.com/explore/tags/brindle/");
$instagram_data = explode("window._sharedData = ", $instagram_source);
$instagram_json = explode(';</script>', $instagram_data[1]);
$instagram_array = json_decode($instagram_json[0], TRUE);
$instagram_media = $instagram_array['entry_data']['TagPage'][0]['tag']['media']['nodes'];
if(!empty($instagram_media)) {
echo '<ul>';
foreach($instagram_media as $im) {
echo '<li>';
echo '<a href="https://www.instagram.com/p/'.$im['code'].'/" target="_blank">';
echo '<img src="'.$im["display_src"].'" alt="" width="'.$im["dimensions"]["width"].'" height="'.$im["dimensions"]["height"].'" />';
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
Take a look at this solution here: https://github.com/Bolandish/Instagram-Grabber
Thats the best one i know until now.
Not sure I worded the title correctly, I'm unsure of what this is called.
I have a while loop which is populated by an SQL query
while($row = $result->fetch_assoc()){
$Tchannel = $row['chan'];
echo '<img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_'.$Tchannel.'-320x180.jpg" alt="Thumbnail"><br />';
echo '<p>';
}
My problem is that when the page loads it does it in sections. I've seen other websites populate these straight away on page load. How would I replicate something like this?
See Example: http://puu.sh/oTh6v/73446c0c15.jpg
Could this be what you meant to achieve...? And by the way, the opening Paragraph Tag:< p > could contribute to some unexpected &/or sluggish behaviour since it was just opened without being closed anywhere in your code... I commented it out... You may try to see if it helps....
<?php
// CREATE A STRING VARIABLE TO HOLD THE ENTIRE OUTPUT TILL YOU ARE READY TO RENDER IT TO THE STREAM...
$strOutput = "";
while($row = $result->fetch_assoc()){
$Tchannel = $row['chan'];
$strOutput .= '<img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_' . $Tchannel . '-320x180.jpg" alt="Thumbnail"><br />';
// WHY DO YOU NEED THIS OPENING PARAGRAPH TAG? WHERE IS IT CLOSED IN YOUR CODE?
//echo '<p>';
}
// RENDER THE OUTPUT
echo $strOutput;
?
If you need the images wrapped in paragraph tags, you can do it differently:
<?php
// CREATE A STRING VARIABLE TO HOLD THE ENTIRE OUTPUT TILL YOU ARE READY TO RENDER IT TO THE STREAM...
$strOutput = "";
while($row = $result->fetch_assoc()){
$Tchannel = $row['chan'];
$strOutput .= '<p>'; // <== OPEN A PARAGRAPH
$strOutput .= '<img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_' . $Tchannel . '-320x180.jpg" alt="Thumbnail"><br />';
$strOutput .= '</p>'; // <== CLOSE THE PARAGRAPH
// OR ALL IN ONE LINE:
// $strOutput .= '<p><img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_' . $Tchannel . '-320x180.jpg" alt="Thumbnail"><br /></p>';
}
// RENDER THE OUTPUT
echo $strOutput;
?>
I am trying to parse a website homepage to convert it into xml file to be used as an api in my app.
So far I have successfully done so. However, the parsed text contains the & (ampersand) character which causes the XML parser to fail.
I am looking for a solution that doesn't use the CDATA or doesn't output CDATA in the XML file.
I want to replace & with and at every occurrence. What phpQuery method should I use?
This causes error in browser because the text() method returns a text with
& character in it.
require('phpQuery/phpQuery.php');
$all=phpQuery::newDocumentFileHTML('BPUT.htm', $charset = 'utf-8');
$links = $all['a.myblue'];
echo '<notice>';
foreach ($links as $link) {
echo '<text>';
echo pq($link)->text();
echo '</text>';
echo '<url>';
echo pq($link)->attr('href');
echo '</url>';
}
echo '</notice>';
?>
I do not want to use CDATA, as the CDATA tag is visible in the generated XML :
<?php
header('Content-type: text/xml');
require('phpQuery/phpQuery.php');
$all=phpQuery::newDocumentFileHTML('BPUT.htm', $charset = 'utf-8');
$links = $all['a.myblue'];
echo '<notice>';
foreach ($links as $link) {
echo '<text>';
echo "<![CDATA[";
echo pq($link)->text();
echo "]]>";
echo '</text>';
echo '<url>';
echo pq($link)->attr('href');
echo '</url>';
}
echo '</notice>';
?>
bumping for answers.
I'm using PHP and a for loop to prepare data into proper html and output the data using JSON to be appended and displayed on the page. JSON slash escaping is causing the html to be viewed incorrectly by the browser.
This is my PHP for loop:
$json = '<div id="rsec3" class="rsec">';
for($i=0; $i<count($array); $i++)
{
$coverart = $array[$i]['cover'];
if(empty($coverart))
{
$coverart = "nocoverart.gif";
}
$json .= '<div><img="/video/cover/thumbs/' . $cover . '"></div>';
}
$json .= '</div>';
$json = json_encode(array('ok' => 'ok', 'html' => $json));
echo $json;
This is my javascript parsing and appending the json:
$.get('/index_get.php?iid='+this.id,function(data){
$('#indload').hide();
js=jQuery.parseJSON(data);
$('#indr').append(js.html);
});
This is what the browser is displaying, a bunch of useless jargon, and it is appending a </img="> on its own?
<img=" video cover thumbs img.png"></img=">
How can I prevent this from occuring, and having the image displayed properly?
I think problem could be invalid HTML tag <img> on the php code. In the <img> tag, src is missing and <img> tag was not closed.
Change the following
$json .= '<div><img="/video/cover/thumbs/' . $cover . '"></div>';
to
$json .= '<div><img src="/video/cover/thumbs/' . $cover . '" /></div>';
you need to close the image tag in your string
$json .= '<div><img="/video/cover/thumbs/' . $cover . '"/></div>';
I'm trying to retrieve feeds from a twitter search to display on various parts of the site. I modified this function to do this yet it works for every feed a try it on except twitter.
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "
<li>
<a href='$entry->link' title='$entry->title'>" . $entry->title . "</a>
</li>";
}
echo "</ul>";
}
I am after only the content of the various associative posts. Here is a sample call.
<?php getFeed("feed://search.twitter.com/search.atom?q=berkshire+golf"); ?>
Any ideas,
Marvellous
Twitter's search API is just a simple HTTP request, so your URL should be http:// and not feed:// if you're using file_get_contents
Edit:
You're also using .atom, use rss instead:
http://search.twitter.com/search.rss?q=berkshire+golf