Facebook feeds on website - php

I'm trying to get some fb feeds on a website. I've searched the web and found a couple things I could try. After failing quite a bit, I've managed to get it. Today the access token wasn't valid anymore, so I got a new one and it worked again. After a bit (token still valid) it didn't anymore (got two messages). So I searched again and found a new solution that works and I think that gives me a token which is longer valid. This is the php code:
<?php
$limit = 3;
$profile_id = "32796xxxxx64243";
//App Info, needed for Auth
$app_id = "59351xxxxx41974";
$app_secret = "082d36fe4108ae51xxxxxxxxxxfb84b4";
//retrieve a new Auth token
$curl = 'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id='.$app_id.'&client_secret='.$app_secret;
//$authToken = file_get_contents($curl);
$authToken = file_get_contents("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");
$data = file_get_contents("https://graph.facebook.com/{$profile_id}/feed?{$authToken}");
echo $data;
?>
This gives me following result:
Now I'm trying to get all the info out of it, but here is where I'm getting an error in my foreach loop. This is the code I use:
<div id="footer">
<h5>Laatste Facebook feeds</h5>
<div class="wrapperfb">
<?
$counter = 0;
foreach($data->data as $d){
if($counter==$limit)
break;
?>
<div class="singlefb">
<div class="imgfb">
<a class="afb" href="http://facebook.com/profile.php?id=<?=$d->from->id?>">
<img border="0" alt="<?=$d->from->name?>" src="https://graph.facebook.com/<?=$d->from->id?>/picture"/>
</a>
</div>
<div class="textfb">
<span style="font-weight:bold"><a class="afb" href="http://facebook.com/profile.php?id=<?=$d->from->id?>">
<?=$d->from->name?></a></span><br/>
<span style="color: #999999;">on <?=date('F j, Y H:i',strtotime($d->created_time))?></span>
<br/>
<?=$d->message?>
</div>
</div>
<?
$counter++;
}
?>
</div>
</div>
This is the error I'm getting:
Warning: Invalid argument supplied for foreach() in
/customers/9/2/3/beach-korfbal.be/httpd.www/facebookFeeds.php on line
19
line 19 is the foreach loop:
foreach($data->data as $d){
It's probably something stupid I'm looking over. Could someone please help me out. This is the last thing I need to complete to launch the site.

It works now thanks to Abhik Chakraborty's comment.
This is the full code:
<?php
$limit = 5;
$profile_id = "32796xxxxx64243";
//App Info, needed for Auth
$app_id = "59351xxxxx41974";
$app_secret = "082d36fe4108ae51xxxxxxxxxxfb84b4";
//retrieve a new Auth token
$authToken = file_get_contents("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");
$data = json_decode(file_get_contents("https://graph.facebook.com/{$profile_id}/feed?{$authToken}"));
?>
<div id="footer">
<h5>Laatste Facebook feeds</h5>
<div class="wrapperfb">
<?
$counter = 0;
foreach($data->data as $d){
if($counter==$limit)
break;
?>
<div class="singlefb">
<div class="imgfb">
<a class="afb" href="http://facebook.com/profile.php?id=<?=$d->from->id?>">
<img border="0" alt="<?=$d->from->name?>" src="https://graph.facebook.com/<?=$d->from->id?>/picture"/>
</a>
</div>
<div class="textfb">
<span style="font-weight:bold"><a class="afb" href="http://facebook.com/profile.php?id=<?=$d->from->id?>">
<?=$d->from->name?></a></span><br/>
<span style="color: #999999;">on <?=date('F j, Y H:i',strtotime($d->created_time))?></span>
<br/>
<?=$d->message?>
</div>
</div>
<?
$counter++;
}
?>
</div>
</div>

Related

Get an id to another page

Hello this is my first post here, I need your help.
First of all, I want to say sorry for my bad English. I hope you will understand me :D
So, I'm a newbie in php and I have a problem.
I want to take the id from a file, to another file, but I don't know how. I will show you my code below.
while($array = $db->get_row($row))
{
$id = $array['id'];
$Nume = $array['user_name'];
$date = date("Y-m-d H:i:s", $array['date']);;
$stitle = $array['shorttext'];
$text = $array['text'];
$logs .= '
<div class="post">
<div class="post-meta clearfix">
<h4 class="post-title left">
'.$stitle.'
</h4>
<p class="post-info right">
<span>'.$Nume.'</span>
'.$date.'
</p>
</div>
<div class="post-box">
<div class="post-content">
<div class="post-intro">
'.$text.'
</div>
</div>
<div class="post-footer clearfix">
<div class="continue-reading">
</div>
</div>
</div>
</div>
<br>
';
}
Here where is ID, I want to take that id to the news page. (the code is in index.php)
'.$stitle.'
In news.php have to select from mysql all but only the table with the ID from index.php
if(isset($_GET['id']))
{
$id = (int)$_GET['id'];
$row = $db->sql("SELECT * FROM `ucp_news` WHERE `id` = '".$id."' ");
$array = $db->get_array($row);
Can you help me, please?
Your PHP code is correct, although you should be using htmlspecialchars() when adding the variables to the output. This to prevent XSS attacks, and the like.
Though, your problem lies most likely in this line:
<a href="/index.php?page=news id='.$id.'">
More specifically, the missing ampersand (&) between news and id. Try to look at the value of $_GET['page'] when clicking on the link, then you should see what the problem is.

php pass id into modal

I'm stuck again! So hopefully you awesome people can help me...
So far what I have is a search function that queries the database, and returns results, which works fine, and then the user can click one of the results, which takes them through to a new detailed page of what they clicked, for example a brand. Then on the detailed page, it returns all of that brands details (using $_GET['id']) and a new set of data, from a relational table, with a list of all the offers corresponding to that brand, which also works. But then I get stuck, I want the user to be able to click on each offer, and have the details of that offer load into a modal... I've tried using the $_GET['id'] for the offers id, but obviously, with it being on the same page (i presume), it passes the id of the brand / parent instead of the offer.
This is the code that calls back all the offers relating to the brand
$offers_sql = 'SELECT * FROM codes WHERE client_id LIKE :client_id AND CURDATE() between start AND expiry';
$offersq = $db->prepare($offers_sql);
$offers_params = array(':client_id' => $id);
$offersq->execute($offers_params);
$results = $offersq->fetchAll(PDO::FETCH_ASSOC);
if(count($results) < 1) {
$output = "<h4>no current offers available</h4>";
}
else {
foreach($results as $r) {
$title = $r['title'];
$code = $r['textcode'];
$offer_id = $r['id'];
$expiry = $r['expiry'];
$date = new DateTime($expiry);
$output .= '<h4>' . $title . '</h4><hr>';
}
}
} catch (PDOException $e) {
echo "failed to load offers";
}
?>
and this is the code i was using for the modal
require('inc/connect/config.php');
include('inc/result.php');
$page_title = "Title - " . $name . " Offers";
include('inc/style.php');
include('inc/header.php');
include('inc/offers.php');
?>
<!-- Intro Section -->
<section class="intro-section">
<div class="container">
<div class="row">
<div class="col-lg-12">
<h3 class="navbar-fixed-top">Latest Deals</h3>
<h1><?php echo $name; ?></h1>
</div>
</div>
</div>
</section>
<section class="offer-section">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-6 col-sm-offset-3 col-md-6 col-md-offset-3 col-lg-6 col-lg-offset-3">
<?php echo $output; ?>
</div>
</div>
</div>
</section>
<div class="md-modal md-effect-flip" id="offer">
<div class="md-content">
<h4><?php $offer = (int) $_GET['id'];
echo $offer ?></h4>
<button type="submit" class="modal-btn md-close navbar-fixed-bottom" value="submit"><p>close</p></button>
</div>
</div>
<?php include('inc/footer.php'); ?>
The code that returns the details of the brand is on a separate page, but i can include that if necessary.
What am i doing wrong? Please go easy on me, I am relatively new to php.
Thanks in advance
Kaylee
Make Changes, Where Output is declared in else condition. (Changes Done)
$output='<h4><a class="OfferIdClass" data-OfferID="<?echo $offer_id;?>" href="#offer" data-modal="offer" data-toggle="modal">'.$title.'</a></h4>';
// Add This to footer.php (Changes Done)
<div class="md-modal md-effect-flip" id="offer">
<div class="md-content">
</div>
</div>
//Add this below in that page, where you are clicking Modal.
//Changes Done
<script>
$('.OfferIdClass').click(function(){
var OfferID=$(this).attr('data-OfferID');
$.ajax({url:"Open-Offer.php?OfferID="+OfferID,cache:false,success:function(result){
$(".md-content").html(result); //Here, Changes Done
}});
});
</script>
//Create one Open-Offer.php page. (Changes Done)
Open-Offer.php
<?
extract($_GET);
<h4><?php $offer = (int) $_GET['id'];
echo $offer ?></h4>
<button type="submit" class="modal-btn md-close navbar-fixed-bottom" value="submit"><p>close</p></button>
?>

Facebook PHP Api suddenly not working

<?php
//get token
$access_token = $facebook->getAccessToken();
$albumCover = $facebook->api("/". $album_id ."?fields=picture", "get");
//echo $albumCover['picture']['data']['url'];
//echo '<pre>';
//print_r($albumCover);
//echo '</pre>';
?>
<div class="fb-album<?php if ($counter % 5 == 0){echo ' no-right';}; ?>">
<a href="<?php the_permalink(); ?>">
<div style="width:160px; height:100px; overflow:hidden;">
<img width="160px" src="<?php echo $albumCover['picture']['data']['url']; ?>" />
</div>
</a>
<p><?php the_title() ?></p>
</div>
All of a sudden this doesnt work, and returns no error, could someone shed some light on this for me?
Thanks
UPDATE:
Found the answer, looks like I could not access the cover photo of each album with ?fields=picture anymore, I had to use ?fields=cover_photo and dig into that array, heres my solution, thanks for all your help STACK
<?php
//get token
$access_token = $facebook->getAccessToken();
$albumCover = $facebook->api("/". $album_id ."?fields=cover_photo", "get");
//echo $albumCover['picture']['data']['url'];
$albumCover2 = $facebook->api("/". $albumCover['cover_photo'] ."?fields=picture", "get");
//echo '<pre>';
//print_r($albumCover2);
//echo '</pre>';
?>
<div class="fb-album<?php if ($counter % 5 == 0){echo ' no-right';}; ?>">
<a href="<?php the_permalink(); ?>">
<div style="width:160px; height:100px; overflow:hidden;">
<img width="160px" src="<?php echo $albumCover2['picture']; ?>" />
</div>
</a>
<p><?php the_title() ?></p>
</div>
I appologize for the formatting, its quite hard to make it look right in this editor
Whenever Facebook API does not get data or does not work,
it logs error in PHP's error log. Check your PHP error log.
There was a breaking changes release today; it's worth checking out the release notes to see how you might have been affected. At a guess, something to do with the privacy changes...
You also need to use setAccessToken method after getAccessToken.
$access_token = $facebook->getAccessToken();
$facebook->setAccessToken($access_token);
As per the error message your have posted in the comment,
If the index active does not exist then you get notice error mod_fcgid. To avoid that you can do check if that index is set / exists.
Code:
if($blah['active']=="something"){foo();}
Should be:
Code:
if(isset($blah['active']) && $blah['active']=="something"){foo();}

Echoing forum Id to new page to display content?

can someone please help me i am having problems creating my forum.
At the moment users can create posts, the post title is listed down the page and then the user is suppose to be able to click the title link and be taken to read_post.php and then this should take the user to another page where the post content can be viewed, i am trying to do this by echoing the forum post id but it doesnt seem to want to work, instead i get this error:
Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
can someonee please show me where im going wrong.
here is my sql function:
function read_forum() {
global $connection;
global $forum_id;
$query = "SELECT *
FROM ptb_forum, ptb_profiles
WHERE ptb_forum.id = $forum_id ";
$forum_set = mysql_query($query, $connection);
confirm_query($forum_set);
return $forum_set;
}
here is the link code that takes the user to read_post.php which suppose to echo the forum id and display the content for each individual post.
<?
$forum_set = get_forum();
while ($forum = mysql_fetch_array($forum_set)) {
?>
<div class="forumcase" id="forumcase">
<div class="pend-forum-content">
<?php echo "<strong>{$forum['title']}</strong> - Posted by {$user['first_name']}"; ?>
</div>
here's my code for read_post.php:
<?php
$page_title = "Read Post";
include('includes/header.php');
include ('includes/mod_login/login_form2.php'); ?>
<?php
confirm_logged_in();
if (isset ($_GET['frm'])) {
$forum_id = $_GET['frm'];
}
?>
<?php include('includes/copyrightbar.php'); ?>
<div class="modtitle">
<div class="modtitle-text">Messages Between <?php echo "{$forum['display_name']}"; ?> & You</div>
</div>
<div class="modcontent57">
<br /><br /><br/><br/>
<div class="forum">
<div class="forum-pic"><?php echo "<img src=\"data/photos/{$_SESSION['user_id']}/_default.jpg\" width=\"100\" height=\"100\" border=\"0\" align=\"right\" class=\"img-with-border-forum\" />";?>
</div>
<div class="message-links">
<strong><< Back to Forum
</div>
<br /><br /><br/><br/>
<?php
$datesent1 = $inbox['date_sent']; ?>
<?php
$forum_set = read_forum();
while ($forum = mysql_fetch_array($forum_set)) {
$prof_photo = "data/photos/{$message['user_id']}/_default.jpg";
$result = mysql_query("UPDATE ptb_forum SET ptb_forum.read_forum='1' WHERE ptb_forum.id='$forum_id'")
or die(mysql_error());
?>
<div class="message-date">
<?php echo "".date('D M jS, Y - g:ia', strtotime($message['date_sent'])).""; ?></div>
<div class="img-with-border-msg-read"><?php echo "<img width=\"60px\" height=\"60px\" src=\"{$prof_photo}\"><br />"; ?></div>
<div class="conversation-text">
<?php echo "<i>Conversations between you and </i>{$forum['display_name']}.<br /> "; ?></div>
<div class="message-content">
<?php echo "<strong>Message Subject: </strong><i>{$forum['subject']}</i>"; ?>
<br/>
<br/>
<br/>
<br/>
<?php echo "<strong>Message:<br/></strong></br ><i>{$forum['content']}</i>"; ?>
</div>
<div class="reply-box">
<? include ('message_reply.php'); ?>
</div>
<?php
}
?>
<br/>
<br/>
<br/>
</div>
</div>
<?php include('includes/footer.php'); ?>
</div>
You have an error in your query... Your parameter is not quoted...
$query = "SELECT *
FROM ptb_forum, ptb_profiles
WHERE ptb_forum.id = '$forum_id'";
However... I suggest that you refrain from using the mysql_ family of functions. They are deprecated and due to be removed from PHP in a future release. You should be using parameterized queries using MySQLi or PDO.
Also, global is evil. I've never had a need to use it in 10 years of PHP programming. Neither should you.

Unexpected issue in internet explorer 9, Assuming that it is caused by html/css

I've started customizing my website recently and it looks alright in all browsers except internet explorer 9. For some reason I get extra header (which is blank) from nowhere. I'm really struggling to find the issue, I tried looking for errors in fire bug, but everything seems to be ok. Also I used compatibility view in internet explorer 9 which shows how website looks in earlier versions of internet explorer and the issue did not accrue there, I suspect it only appears in ie9 maybe ie8.
http://inelmo.com
if you visit my website from ie9 and other browser e.g. chrome you can see the issue clearly. I have no idea about what is causing it, I assume it is something to do with html/css , but it could be something in php I use to get articles.
PHP
$sql = mysql_query("SELECT * FROM stories WHERE showing = 1 ORDER BY st_date DESC LIMIT 10") or die (mysql_error("There was an error in connection"));
$storyDisplay = "";
while($row = mysql_fetch_assoc($sql)){
$story_id = $row["id"];
$story_author_name = $row["st_auth"];
$date = $row["st_date"];
$convertedTime = ($myObject -> convert_datetime($date));
$whenStory = ($myObject -> makeAgo($convertedTime));
$story_title = $row["st_title"];
$story_category = $row["st_category"];
$the_story = $row["st_body"];
$verified = $row["showing"];
if ($verified = 1) {
$verified_class = 'class="verified" title="Good Post!"';
} else {
$verified_class = '';
}
//Create random Style
$style_class = array("st_style1","st_style2","st_style3","st_style4");
$random_class = array_rand($style_class, 1);
$div_class = $style_class[$random_class];
//Create random image
$topic_img = array("s1","s2","s3","s4","s5","s6","s7","s8","s9","s10","s11","s12","s13","s14","s15");
$random_img = array_rand($topic_img, 1);
$the_img = $topic_img[$random_img];
$storyDisplay .= '
<div class="'.$div_class.'">
<div class="st_header">
<div class="st_info">
<img class="st_image" src="images/smile/'.$the_img.'.png" alt="image of '.$story_author_name.'"/>
<div class="st_title">'.$story_title.'</div>
<div class="st_auth">by: '.$story_author_name.'</div>
</div>
<div class="st_vote">'.$pulse->buttons($story_id).'</div>
</div>
<div class="st_body">
<div class="st_container">
<article>'.$the_story.'</article>
</div>
</div>
<div class="st_footer">
<ul>
<li><time '.$verified_class.' datetime="'.$date.'">'.$verified_img.''.$whenStory.'</time></li>
<li class="st_category"><a class="st_categorya" href="#">'.$story_category.'</a></li>
<li class="st_id"><a class="st_ida" href="#">'.$story_id.'</a></li>
<li class="st_comments"><a class="st_commentsa" href="http://inelmo.com/view_story.php?id='.$story_id.'">Comments</a></li>
</ul>
<div class="st_social">
<div class="addthis_toolbox addthis_default_style"
addthis:url="http://inelmo.com/view_story.php?id='.$story_id.'"
addthis:title="'.$story_title.'"
addthis:description="'.$the_story.'">
<a class="addthis_button_preferred_1"></a>
<a class="addthis_button_preferred_2"></a>
<a class="addthis_button_preferred_3"></a>
<a class="addthis_button_preferred_4"></a>
<a class="addthis_button_preferred_5"></a>
<a class="addthis_button_compact"></a>
<a class="addthis_counter addthis_bubble_style"></a>
</div>
</div>
<br />
</div>
</div><br />
'; // End $storyDisplay
}// Close main while loop
w3c validator gives 12 errors, mostly unallowed child relations and, therefore, wrongly opened/closed tags. You might start working on that:
http://validator.w3.org/check?uri=http%3A%2F%2Finelmo.com%2F&charset=%28detect+automatically%29&doctype=Inline&group=0

Categories