I am grabbing the comments from videos, but the "author name" and their id is giving NULL.
This is what I have.
$feedUrl='http://gdata.youtube.com/feeds/api/videos/'.$selected_video_id.'/comments?v=2&alt=json';
$data = json_decode(file_get_contents($feedUrl),true);
$info = $data["feed"];
$entry = $info["entry"];
$nEntry = count($entry);
for($i=0;$i<$nEntry;$i++){
$name = $entry[$i]['author']['name']['$t'];
$userId = $entry[$i]['author']['yt$userId']['$t'];
$content = $entry[$i]['content']['$t'];
$published_2 = $entry[$i]['published']['$t'];
}
Content and Published are collected fine, but the name and userID are not.
The feed does have the elements in it as have looked at it in the youtube data api demo beta. As well as it shows everything if you do a feed request in the browser.
http://gdata.youtube.com/feeds/api/videos/VIDEO_ID/comments
So am I missing something?
It should be:
$name = $entry[$i]['author'][0]['name']['$t'];
$userId = $entry[$i]['author'][0]['yt$userId']['$t'];
$content = $entry[$i]['content']['$t'];
$published_2 = $entry[$i]['published']['$t'];
Or better yet:
$feedUrl=file_get_contents('http://gdata.youtube.com/feeds/api/videos/ASO_zypdnsQ/comments?v=2&alt=json');
$json = json_decode($feedUrl, true);
foreach($json['feed']['entry'] as $entry) {
echo $entry['author'][0]['name']['$t']."<br>";
echo $entry['author'][0]['yt$userId']['$t']."<br>";
echo $entry['content']['$t']."<br>";
echo $entry['published']['$t']."<br>";
}
You can use foreach like the above :)
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.
I have this code but I can't make the items to show properly, I can only show the data from echo $data sentence but not $item FirstName or Item Bio
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID=17965'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$items = json_decode($data, true);
foreach ($items as $item)
{
$name = $item = ["FirstName"];
$bio = $item = ["Bio"];
}
echo $data
if you see the json response it has 2 elements.
{
message : ...,
department : ....
}
so you have to loop throw $items['department'] rather than $items
and you can access the data like $item['Bio']
So you can change the code like this to get the information
<?php
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID=17965';
$data = file_get_contents($url); // into a variable
$items = json_decode($data, true); // decode the JSON feed
foreach ($items['department'] as $item)
{
$name = $item["FirstName"];
$bio = $item["Bio"];
echo $name . ' ' . $bio . PHP_EOL;
}
I want to turn the variable into an array so I can store more than one feed?
<?php
error_reporting(0);
$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');
$xml = $feed_lifehacker_full;
//print_r($xml);
foreach ($xml->channel->item as $node){
$title = $node->title;
$link = $node->link;
$link = explode('/', $link);
$link = $link[8];
$url = $node->url;
$description = $node->description;
$pubDate = $node->pubDate;
preg_match_all('#(http://img[^\s]+(?=\.(jpe?g|png|gif)))#i', $description[0], $images);
$images = $images[0][1] . '.jpg';
if($images == '.jpg'){
//uncomment to show youtube articles
//$images = "http://placehold.it/640x360";
//echo "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
} else {
//article image
$images . '<br>';
echo "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
}
}
How can I change this to load to arrays,
$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');
$xml = $feed_lifehacker_full;
The script is just gathering the image of an rss feed and linking to a page, if you see how it can be done more efficiently feel free to say
it is possible to encode the result given as json and by decoding it it will return you an array
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json, TRUE);
I have an rss feed, created by Yahoo Pipes and I need to get random post from it. How is it possible to realize this on php?
Read the feed using XML Parser and put it in an array. then, use array_rand to pick a random item from the array.
<?
function load_xml_feed($feed)
{
global $RanVal;
$i= 1;
$FeedXml = simplexml_load_file($feed);
foreach ($FeedXml->channel->item as $topic) {
$title[$i] = (string)$topic->title;
$link[$i] = (string)$topic->link;
$description[$i] = (string)$topic->description;
$i++;
}
$randtopic = rand(2, $i);
$link = trim($link[$randtopic]);
$title = trim($title[$randtopic]);
$description = trim($description[$randtopic]);
$RanVal = array($title,$link,$description);
return $RanVal;
}
$rss = "http://www.sabaharabi.com/rss/rss.xml";
load_xml_feed($rss);
$link = $RanVal[1];
$title = $RanVal[0];
$description = $RanVal[2];
echo "<h1>".$title."</h1><h2>".$link."</h2><p>".$description."</p>";
i want to create dynamic banner rotater wih php ajax i want to pass the mysql_fetch_array() to an array to create a new array() to create xml response..........
here is my code
$sql = mysql_query("SELECT * FROM ads");
header('Content-type: text/xml');
echo '<?xml version="1.0" ?>';
while($row = mysql_fetch_array($sql)){
$title = $row['title'];
$img = $row['file'];
$body = $row['body'];
$ban = '<b>'.$title.'</b><br><br><img src="ads/'.$img.'"><br><br>'.$body;
$banners = array(
$ban,
);
$html = $banners[array_rand($banners)];
}
<banner>
<content><?php echo htmlentities($html); ?></content>
<reload>3000</reload>
</banner>
but it is return only one ad not return multiple ads how can i fix that
The proble is here: $banners = array($ban);. What you're trying to do is include all the ads in the $banners array as an entry but you are failing to achieve that.
The correct code for including an entry in an array would be $banner[] = $ban. That way each ad that comes as a result from your query will be stored as an individual entry.
So the correct code would be:
$sql = mysql_query("SELECT * FROM ads");
$banner = array(); //Define the array before trying to add elements.
header('Content-type: text/xml');
while($row = mysql_fetch_array($sql))
{
$title = $row['title'];
$img = $row['file'];
$body = $row['body'];
$ban = '<b>'.$title.'</b><br><br><img src="ads/'.$img.'"><br><br>'.$body;
$banner[] = $ban; //Adding a new entry at the end.
$html = array_rand($banner); //Getting a random entry.
}
Use this in the while loop:
$banners[] = $ban
Instead of
$banners = array(
$ban,
);
and
$html = array_rand($banners);
instead of
$html = $banners[array_rand($banners)];
And to what bzabhi said, define your
$banners = array();
before the while loop, and the randomization part has to go after the loop.