Facebook Graph 2.9 Have Square Images - php

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.

Related

RSS images using SimpleXml not displaying.Works for .xml RSS feeds but not with .cms

I get
main(): Node no longer exists
while trying to display images (media:content)from a Rss url. I have been trying to solve it but was not successful . Can someone explain me Why this error is displaying and Help me solve it.the code works well if I Parsed RSS Feeds Urls with a .xml extension but not with a .cms Extension Urls.I Dont know why this is happening or whats wrong with my CODE.
My View
foreach ($data1 as $key1 => $value1) {
$image=$data1[$key1]['image']->children('http://search.yahoo.com/mrss/')->content->attributes()->url;
echo '<p>'.$image.'</p>';
echo '<div class="col-lg-6">';
echo '<img src="'.$image.'" /><strong style="color:pink;">'.$data1[$key1]["title"].'</strong>';
echo '<p>'.$data1[$key1]["description"].'</p>';
echo '<p>'.$data1[$key1]["pubDate"].'</p>';
echo '</div>';
}
My Controller
foreach($urls as $key => $url){
$xml = simplexml_load_file($url);
$result[$key]['title'] = $xml->channel->title;
$data = [];
for ($i=0; $i<5 ; $i++) {
# code...
$items = [];
if($xml->channel->item[$i] == null)
{
break;
}
$items['title'] = $xml->channel->item[$i]->title;
$items['link'] = $xml->channel->item[$i]->link;
$items['description'] = $xml->channel->item[$i]->description;
$items['pubDate'] = $xml->channel->item[$i]->pubDate;
$items['image'] = $xml->channel->item[$i];
$data[$i] = $items;
}
$result[$key]['data'] = $data;
//$entries = array_merge($worldFeed,$entries);
}
return View::make('index')->with('result',$result);

Array to string conversion issue works before submitting form but not after

Hello I am having trouble with array to string conversion in the following script.
The problem is on the top if statement. For some reason it works fine in the else statement underneath but I keep getting a array to string conversion error when submitting my form and I'm not sure why it works before submitting the form but not after.
The problem is apparently in the first
$content[] = array.
It just returns the word array for $video variable. So where it states the videoid in data-videoID="" it just comes up as data-videoId="Array". Everything esle comes up fine. This would be on line 12 and 13. Here is the code I am using. Im sure it probably something simple that I am overlooking because I am in no way as experienced with php as I probably should be. I have looked over other posts and have tried taking off the brackets and several other things but still cant figure it out. Like I said it works fine if I take out the if statement and just use the else statement. Here is the code.
if(isset($_GET['q'])) {
$keyword_q = $_GET['q'];
$keyword = preg_replace('/\s+/', '/', $keyword_q);
$file = file_get_contents("https://www.googleapis.com/youtube/v3/search?videoEmbeddable=true&videoType=any&part=id%2Csnippet&q=$keyword&videoSyndicated=true&type=video&maxResults=50&key=$key");
$decoded = json_decode($file, true);
$entries = $decoded['items'];
if (!empty($entries)) {
for($i=0; $i<count($entries); $i++) {
$thumb = $entries[$i]['snippet']['thumbnails']['medium']['url'];
$videotitle = $entries[$i]['snippet']['title'];
$videodescription = $entries[$i]['snippet']['description'];
$video = $entries[$i]['id'];
$content[] = "<li><a href='#' data-videoID='$video' class='video-link'><img src='$thumb' alt='Play Video'></a><h3 class='video-title'>$videotitle</h3><p class='video-description'>$videodescription</p></li>";
}
}
$videos = "";
if (!empty($content)) {
foreach($content as $thumb){
$videos .= "$thumb";
}
}
}
else {
$filefeatured = file_get_contents("https://www.googleapis.com/youtube/v3/videos?chart=mostPopular&part=snippet&maxResults=30&key=$key");
$decoded = json_decode($filefeatured, true);
$entries = $decoded['items'];
if (!empty($entries)) {
for($i=0; $i<count($entries); $i++) {
$thumb = $entries[$i]['snippet']['thumbnails']['medium']['url'];
$videotitle = $entries[$i]['snippet']['title'];
$videodescription = $entries[$i]['snippet']['description'];
$video = $entries[$i]['id'];
$content[]= "<li><a href='#' data-videoID='$video' class='video-link'><img src='$thumb' alt='Play Video'></a><h3 class='video-title'>$videotitle</h3><p class='video-description'>$videodescription</p></li>";
}
}
$videos = "";
if (!empty($content)) {
foreach($content as $thumb){
$videos .= "$thumb";
}
}
}

Youtube feed array items in json showing as NULL

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 :)

Loading content from remote site doesn't work, but why?

I'm still working on this catalogue for a client, which loads images from a remote site via PHP and the Simple DOM Parser.
// Code excerpt from http://internetvolk.de/fileadmin/template/res/scrape.php, this is just one case of a select
$subcat = $_GET['subcat'];
$url = "http://pinesite.com/meubelen/index.php?".$subcat."&lang=de";
$html = file_get_html(html_entity_decode($url));
$iframe = $html->find('iframe',0);
$url2 = $iframe->src;
$html->clear();
unset($html);
$fullurl = "http://pinesite.com/meubelen/".$url2;
$html2 = file_get_html(html_entity_decode($fullurl));
$pagecount = 1;
$titles = $html2->find('.tekst');
$images = $html2->find('.plaatje');
$output='';
$i=0;
foreach ($images as $image) {
$item['title'] = $titles[$i]->find('p',0)->plaintext;
$imagePath = $image->find('img',0)->src;
$item['thumb'] = resize("http://pinesite.com".str_replace('thumb_','',$imagePath),array("w"=>225, "h"=>162));
$item['image'] = 'http://pinesite.com'.str_replace('thumb_','',$imagePath);
$fullurl2 = "http://pinesite.com/meubelen/prog/showpic.php?src=".str_replace('thumb_','',$imagePath)."&taal=de";
$html3 = file_get_html($fullurl2);
$item['size'] = str_replace(' ','',$html3->find('td',1)->plaintext);
unset($html3);
$output[] = $item;
$i++;
}
if (count($html2->find('center')) > 1) {
// ok, multi-page here, let's find out how many there are
$pagecount = count($html2->find('center',0)->find('a'))-1;
for ($i=1;$i<$pagecount; $i++) {
$startID = $i*20;
$newurl = html_entity_decode($fullurl."&beginrec=".$startID);
$html3 = file_get_html($newurl);
$titles = $html3->find('.tekst');
$images = $html3->find('.plaatje');
$a=0;
foreach ($images as $image) {
$item['title'] = $titles[$a]->find('p',0)->plaintext;
$item['image'] = 'http://pinesite.com'.str_replace('thumb_','',$image->find('img',0)->src);
$item['thumb'] = resize($item['image'],array("w"=>225, "h"=>150));
$output[] = $item;
$a++;
}
$html3->clear();
unset ($html3);
}
}
echo json_encode($output);
So what it should do (and does with some categories): Output the images, the titles and the the thumbnails from this page: http://pinesite.com
This works, for example, if you pass it a "?function=images&subcat=antiek", but not if you pass it a "?function=images&subcat=stoelen". I don't even think it's a problem with the remote page, so there has to be an error in my code.
Ehm..trying to state the obvious maybe but 'stoele'?
As it turns out, my code was completely fine, it was a missing space in the HTML of the remote site that got the Simple PHP DOM Parser to not recognize the iframe I was looking for. I fixed it on my end by running a str_replace on the code first to replace the faulty code.
I know it's a dirty solution, but it works :)

Get Twitter feed and display information using SimpleXML

Based on the thread With PHP preg_match_all, get value of href, I'm trying to get some information from a twitter feed.
Here is the feed url (for testing purpose): Twitter feed
Here is my code:
function parse_feed($process) {
$xml = #simplexml_load_string($process);
$findTweet = $xml['entry'];
return $findTweet;
}
$username = 'tweet';
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=2";
$feed = file_get_contents($feed);
//echo $feed;
print_r(parse_feed($feed));
I never used SimpleXML before or worked with XML.
Can someone help me please?
this site might be a good start http://www.php.net/manual/en/simplexml.examples-basic.php
Okay Founf it!, here is the solution for who is interested...
Thanks to m1k3y02 for the docs!
function parse_feed($process) {
$xml = new SimpleXMLElement($process);
$n=0;
foreach($xml->entry as $entry) {
$tweets[$n] = array($entry->published,$entry->content);
$n++;
}
return $tweets;
}
$twitter_username = 'tweet';
$twitter_entries = 5;
$feed = "http://search.twitter.com/search.atom?q=from:" . $twitter_username . "&rpp=".$twitter_entries;
$feed = file_get_contents($feed);
$tweets = parse_feed($feed);
$n=0;
$n_t = count($tweets);
while($n < $n_t) {
echo "<div class=\"tweet\"><img src=\"img/tweet.png\" valign=\"absmiddle\" /> ";
echo $tweets[$n][1][0];
echo "</div>";
echo "<div class=\"date\">".$tweets[$n][0][0]."</div>";
$n++;
}

Categories