Count of comments and likes are not shown in feed and comments - php

Hy well how does the url looks when you use a PageID and at the same time want the count of all comments and likes back I tried it hard way
$feed_profile = $facebook->api('/'.$row[0].'/feed&access_token=$access_token&limit=500',"get");
$post_profile = $facebook->api('/'.$row[0].'/comments?access_token=$access_token&limit=500',"get");
also tried comments& instead of comments?
I dont know how to get back the comments and likes what must I exactly do my var $row[0] stores various Pages from fb

the feed has connections EG: comments, likes that provide the count.
$feed_profile = $facebook->api('/'.$row[0].'/feed&access_token=$access_token&limit=5',"get");
// get comment/like count for first post in array. this can be done with a loop.
echo 'Comments: '.$feed_profile[data][0][comments][count].'<br />';
echo 'Likes: '.$feed_profile[data][0][likes][count].'';
NOTE: you will have to account for error handling, when no comments or
likes have been made the connections arrays do not exsist

Related

Facebook Graph api get Posts starting from x post (PHP SDK)

Hello i've serched a lot without find an answer to this.
is possible to retrieve Fb posts of a certain public page starting from a post id?(for example tomorrow start collecting posts from the last post of today)
Let's take for example Bill Gates page
the idea would be to do something similar to:
$id = 216311481960; //Bill's fb id
$last_post = 10153219939326961 // last bill posts
try {
$request= $id.'/posts?since='.$last_post.'&access_token=xxxxxxxxxxxxxxxxxxxx|xxxxxxxxxxxxxxx-xxxxxx';
$search = $fb->get($request);
if (isset($search)){
//do something
}
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage()."<h1>".$id."</h1>";
}
obviously since parameter has to be a date and that code wont work.
Any Idea?
Thanks in advance.
Edit
i try to search for all posts of a specific Facebook user starting not at a certain date but from a certain post, lets say that today i grab users postid1,postid2,postid3 (postid3 is the last post i've taken, and i save that id) next time i would to grab new posts published after postid3 (since postid3)
You can do it in the url, I just made it work recently. This is from a python code, but the URL should work the same way.
"https://graph.facebook.com/?ids=" + ids[:-1] +
"&fields=name,posts.limit(5).since(10+days+ago)" +
"{from,to,full_picture,message,caption,description," +
"link,name,shares,likes.summary(true)," +
"comments.summary(true),created_time,is_hidden," +
"message_tags,place,type,with_tags}")
This will get the last 5 posts from the last 10 days for several page ids. It will also get the total number of likes and comments as summary.
It took me a while to dig this info from the net, I found that since= has changed to since().
I tried to retrieve some reference where it's documented, so far with no success. I'll give you an update if I find it.

retrieving data from database with php and send to another page

I am now trying to create a page which shows restaurants in a list for people to comment and rate, the restaurant info is in the database. I've already get the data I want using while() function, but I am struggling to pick one of them and pass to another page. Below is my code. I tried to use sessions to store the data, like the "$_SESSION['rid']" for storing the restaurant ids. I have 8 rows in the restaurant table, and when I click on the first restaurant, the number shows on the other page is 8.
<?php
$sql_restaurant = "select * from tbl_restaurants";
$results = mysql_query($sql_restaurant);
while($restaurant_row=mysql_fetch_assoc($results)){
$_SESSION['rid'] = $restaurant_row['restaurant_id'];
echo "<a><span style = 'color:black'>".$restaurant_row['restaurant_name']."</span></a>";
echo "<dd>";
echo "<a><span style = 'color:black'>".$restaurant_row['restaurant_address']."</span></a>";echo '</dd>';
echo '<i class="mod_subcate_line"></i>';
echo 'Rate it!';
echo 'Comment!';
echo '<br/>';
echo '<br/>';
}
?>
I want it to show the right restaurant id when I click on different restaurants. How can I solve this?
Right now your code is changing $_SESSION['rid'] every time you cycle through the loop. So it will have the last cycle of the loop.
You shouldn't do this with sessions, always keep in mind that HTTP is a stateless system and sessions are stateful. It's always better to not rely on state.
As mentioned in the comments, you should write the restaurant ID as part of the URL of the link in a query parameter that you can then access through the $_GET array.
If you want to use $_SESSION, choose a different name for every loop. Here, $_SESSION['rid'] get the value of the last loop.

Trying to pull replies of comments from facebook with php and json

I am trying to pull a public page from facebook using php and json to sort the data received. I have everything working perfectly apart from replies on comments on posts. I can get all the list of comments on a post but the replies to that comment do not appear on the json data that is returned to me. I believe when the user of the page comments on his own post the story tag is given to the comment and it is unrelated to the comments section of that post. But if anybody else replies to a comment i cannot see their message anywhere. Is there any way to try and get the replies of comments returned in json along with everything else?
edit: found a better solution
see the answer here: How to get the image from a facebook photo comment?
the query you want to perform is
/{page-post-id}/comments?fields=from,message,id,attachment,created_time,comments.fields(from,message,id,attachment,created_time)
to query this via PHP you would do something like this:
$page_comment_id = 12345; // put your id here
$access_token = ''; // put your facebook access token here
$url = 'https://graph.facebook.com/' . $page_comment_id . '/comments?fields=from,message,id,attachment,created_time,comments.fields(from,message,id,attachment,created_time)&access_token=' . $access_token;
$data = json_decode(file_get_contents($url),true);
print_r($data);

Find users likes a page : Best method?

$likes = $facebook->api("/me/likes");
foreach($likes['data'] as $page_likes) {
if($page_likes['id'] == "someid") {
}
}
Is there any simple way to find it instead of looping those hundreds, thousands likes?
Best method (to answer questions like this): Read documentation …!
https://developers.facebook.com/docs/reference/api/user/#likes:
“You can check if a User likes a specific page by issuing an HTTP GET to /PROFILE_ID/likes/PAGE_ID. […] This will return, in the data array, an object with the following fields if the user is connected to the page: […] If the user is not connected to the page, the data array will be empty.”
array_map or array_walk might be more efficient, but it depends on what you want to do with the data...

Displaying name instead of ID PHP MySQL

I need something simple; I have page where a user clicks an author to see the books associated with that author. On my page displaying the list of books for the author, I want a simple HTML title saying: 'The books for: AUTHORNAME'
I can get the page to display author ID but not the name. When the user clicks the link in the previous page of the author, it looks likes this:
<?php echo $row['authorname']?>
And then on the 'viewauthorbooks.php?author_id=23' I have declared this at the start:
$author_id = $_GET['author_id'];
$authorname = $_GET['authorname'];
And finally, 'The books for: AUTHORNAME, where it says AUTHORNAME, I have this:
echo $authorname
(With PHP tags, buts its not letting me put them in!) And this doesnt show anything, however if I change it to author_id, it displays the correct author ID that has been clicked, but its not exactly user friendly!! Can anyone help me out!
You could pull the author_id from the query string as you did using $_GET but beware you will need to validate what is coming through by the query. I hope you can see that without validation how bad of a security hole this is.
I am at work at the moment, but this is a quick example that should give you what you need without sanitizing your query.
$id = intval($_GET['author_id']);
// of course, perform more validation checks
// just don't assume its safe.
$sql = "SELECT authorname FROM authors_tb WHERE author_id=" . $id;
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo "The books for: " . $row['authorname'];
}
The reason why your approach wasn't working was because you utilize the $_GET URL parameter passing for author_name where you weren't supplying the parameters in the URL, just the author_id.
You don't send it in the query string, thus you can't get it from the $_GET array.
Just request it from the database using id.
An important note: Always use htmlspacialchars() when you display the data, coming from the client side.
This is because you do not define the author name in your get.
You should make the following your url:
<?php echo $row['authorname']?>
Or rather select the data from the database again, on the new page, using the ID you retrieved from the URI.
Author name won't be in $_GET. As your code stands, you only use it as the link title. It is no where in the address. Try this instead:
<?php echo $row['authorname']?>
It would be better to re-request it from the database using the author_id though.
EDIT:
To explain the problem in more detail. You have two pages, the new.php page and the viewauthorbooks.php page. You're sending users from the new page to the view page using the link you posted, right?
The problem with that is, your link assigns one variable in get. Here's the query string it would generate:
viewauthorbooks.php?author_id=13
What that will do is send the user to viewauthorbooks and place the value '13' in the $_GET variable: $_GET['author_id']. That is why the author_id is there and displays on viewauthorbooks. However, authorname is never passed to viewauthorbooks, it isn't in $_GET['authorname'] because you never set $_GET['authorname']. If you want it to be in $_GET, then you need your query string to look like this:
viewauthorbooks.php?author_id=13&authorname=bob
You can accomplish that using the new HTML code for the link I posted above. Look at it closely, there's a key difference from the one you have now.
However, it is generally discouraged to pass data through GET, because the query string is displayed to the user and it leaves you open to injection attacks. A better way to do this would be to use the author_id you are already passing to viewauthorbooks.php to retrieve the authorname from the database again. You can use the same code you used on the new.php page.

Categories