How do I echo something from Facebook's graph api? - facebook

I want to display the content from: https://graph.facebook.com/comments/?ids=http://lujanventas.com/test.php in my site.
How can I do it aside from an AJAX call? Can it be done without JS?

you can create a server side script that reads the content of the file and echos it
this is an example for such script in php:
$ids = "http://lujanventas.com/test.php";
$url = "https://graph.facebook.com/comments/?ids=".urlencode($ids);
$data = json_decode(file_get_contents($url),true);
foreach($data[$ids]["comments"]["data"] as $comment){
echo $comment["message"]."<br /><br />";
}

Related

Test Rest API with file_get_contents

I am having issues testing a rest API. I want to trigger it from PHP by doing a file_get_contents.
This is my code so far
<?php
$url = 'http://domain:0000/rest/createUser?u=username&p=password&username=testuser&password=testpassword&email=user#domain.co.uk&';
$encodedUrl = urlencode($url);
$apicall = file_get_contents($url);
?>
This URL works from a browser, but as soon as I use file_get_contents it doesn't work.
Could the end server be blocking the use of file_get_contents? If so how? and how can I begin to test and troubleshoot this?
The issue is that you're using urlencode.
The urlencode function is used specifically for encoding strings inside a portion of a url. For example, you can use it to add data after the ?, and make sure that things like & turns into %26 and spaces turn into %20.
But it's not used to encode the entire url, it just makes the url invalid.
Try to remove the last "&" in your URL and then use the $encodedUrl instead of $url in the file get contents.
So try to turn this :
$apicall = file_get_contents($url);
INTO
$apicall = file_get_contents($encodedUrl);

Facebook page tab app session across subpages PHP SDK 4

See the full original question further down
Using the latest Facebook PHP SDK 4.4.0, in my main app page I can do the following to get a user id etc.
<?php
FacebookSession::setDefaultApplication(APP_ID, SECRET);
$helper = new FacebookRedirectLoginHelper( PAGE_URL );
$pageHelper = new FacebookPageTabHelper();
$session = $pageHelper->getSession();
echo '<p>You are currently viewing page: '. $pageHelper->getPageId() . '</p>';
// get user_id
echo '<p>User Id: ' . $pageHelper->getUserId() . '</p>';
// **depcrecated** get like status - use for likegates
echo '<p>You have '. ( $pageHelper->isLiked() ? 'LIKED' : 'NOT liked' ) . ' this page</p>';
// get admin status
echo '<p>You are '. ( $pageHelper->isAdmin() ? 'an ADMIN' : 'NOT an ADMIN' ) . '</p>';
?>
This does not work on sub pages of my app ... Why is the session (and amongst other things, the signed request) lost? How can I get them back and how can I get methods such as getUserId() from the the FacebookPageTabHelper to continue to work on sub pages?
full original question
I'm fairly new to Facebook app development and I'm having problems with session management and I just can't seem to be able to wrap my head around it. Of course it doesn't help that the official documentation is almost useless.
My problem is that the page session get lost when moving away from the apps main page to a subpage within the Facebook page tab app iframe.
I use the following PHP code to obtain the session and user id on the main (initial) app page and it works great:
<?php
FacebookSession::setDefaultApplication(APP_ID, SECRET);
$helper = new FacebookRedirectLoginHelper( PAGE_URL );
$pageHelper = new FacebookPageTabHelper();
$session = $pageHelper->getSession();
?>
But it doesn't work on sub pages :( when a user clicks on a menu item (or any other link inside the app/iframe), the session goes bye bye. Which is not ideal as I need the user id of the user to track whether or not that user has completed certain actions. Of course I could send the ID along with every request, but there must be a way to have a persisting session, no?
Is there a way to retrieve the session on a sub page in PHP? If so, how? Or do I have to load additional content using javascript? And how would that work, if I can't keep the session between requests and therefore have no way of identifying which user a request came from? How do others handle this?
What I'd like to avoid is to write my own user session management, which would solve the problem but is simply not in the budget and I was hoping I could work with what Facebook already had on offer. Especially since my app doesn't require user information/permissions of any kind.
Thanks a lot in advance for any info on this topic, greatly appreciated, going in circles here.
Edit to clarify: I thought of just saving the Facebook session in a PHP session cookie, but how would I use that to reconnect with Facebook after changing the page?
I finally managed to solve this problem. I'm not sure whether this is considered the right way or can even be a recommended way of doing this, but it works and since time is of the essence, I don't have much of a choice.
If anybody has any further ideas or suggestions, please comment.
Here's how I did it:
// store the signed request
if(isset($_REQUEST['signed_request'])) {
$_SESSION['signed_request'] = $_REQUEST['signed_request'];
} elseif($_SESSION['signed_request']) {
$_REQUEST['signed_request'] = $_GET['signed_request'] = $_POST['signed_request'] = $_SESSION['signed_request'];
}
// assign the stored signed request to REQUEST, GET and POST vars (the unsavory bit, imo)
$_REQUEST['signed_request'] = $_GET['signed_request'] = $_POST['signed_request'] = $_SESSION['signedRequest'];
FacebookSession::setDefaultApplication(APP_ID, APP_SECRET);
$accessToken = APP_ID . '|' . APP_SECRET;
$this->session = new FacebookSession($accessToken);
$pageHelper = new FacebookPageTabHelper();
$isAdmin = ($this->pageHelper->getPageData('admin')) ? $this->pageHelper->getPageData('admin') : 0;
// get pade id
echo '<p>You are currently viewing page: '. $pageHelper->getPageId() . '</p>';
// get user_id
echo '<p>User Id: ' . $pageHelper->getUserId() . '</p>';
// get admin status
echo '<p>You are '. ( $isAdmin ? 'an ADMIN' : 'NOT an ADMIN' ) . '</p>';

Grab Bing Result From Search Pages

I want to get the bing result without api , I want to get them direct from bing pages using php. I don't know how to do it.
you can use http requests and pares the results to get what you want
you can use regular expressions
example:
$results = file_get_contents("http://www.bing.com/search?q=regular+expressions");
you will find the all HTML content of the page, like you see when click on "view page source"
then apply regex on it to extract the results
Check out this script: http://www.fromzerotoseo.com/scraping-bing-serp/
in particular the regular expression search which grabs the links:
preg_match_all(
'(<div class="sb_tlst">.*<h3>.*<a href="(.*)".*>(.*)</a>.*</h3>.*</div>)siU',
$result['EXE'], $matches);
Just use simple_html_dom and do a simple parse. To get # of bing results, you have to find a specific class called "sb_count". If you need links, change the class by "b_attribution" to get all the links.
You can execute your query as follow by looping through the result page using foreach:
include_once 'simple_html_dom.php';
header('Content-Type: text/html; charset=ISO-8859-2');
$html = new simple_html_dom();
$param = 'Your query';
$html -> load_file('https://www.bing.com/search?q=' . $param . '&go=Submit&qs=n&form=QBLH&pq=' . $param . '&sc=8-6&sp=-1&sk=&ghc=1&cvid=e3777d60b1f04c90a3d8f08903433c7a');
foreach ( $html->find('.sb_count') as $post){
echo '<p>' . $post . '</p>';
}

Cant tell if phpactiverecord is connecting to database

I am writing a web service for a droid app adn trying to use phpactiverecord. I am not using an mvc. I cannot tell whether it is connecting to my database correctly because I don't know what it returns when it fails or if it just can't be used without an mvc.
<?php
require_once '../libs/php-activerecord/ActiveRecord.php';
ActiveRecord\Config::initialize(function($cfg)
{
$cfg->set_model_directory('../libs/php-activerecord/models');
$cfg->set_connections(array(
'development' => 'mysql://username:password#localhost/dbname'));
});
//$json=$_GET ['json'];
$json = file_get_contents('php://input');
$obj = json_decode($json);
//echo $json;
$data = Users::all();
$user_name = $data->user_name;
echo($data);
$password = $data->password;
$posts = array(name =>$user_name,
password =>$password
);
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
?>
It throws a 500 error when I try to go to the page which isn't very helpful.
I figured it out but thanks for trying to help. My hosting server has a wierd structure that required my path to include a bunch of extra stuff that would be unlikely to happen to anyone else... basically my include pat was wrong. turning on error reporting pointed out the problem quickly.

sphotos.xx.fbcdn.net Issue With Full Size Profile Pictures

I'm stuck with what is causing this issue. If i'm logged in as my account, the code below does not show user: 100000920350966 but if i logout & back into my wife's account I can now see that users profile picture.
Error I get logged into my FB account:
http://sphotos.xx.fbcdn.net/hphotos-snc6/230962_10150332269178009_784368008_9751025_7802946_n.jpg
An example of one that does work:
http://a4.sphotos.ak.fbcdn.net/hphotos-ak-ash4/419763_10151360491990078_877370077_23594484_2739927_n.jpg
the code I am using is the PHP SDK and this piece for getting an image:
function randomUser() {
$query = mysql_query("SELECT the_facebook_id FROM users WHERE the_facebook_id != '$facebook_id' ORDER BY RAND() LIMIT 0,1");
$result = mysql_fetch_array($query);
mysql_close();
return $result; }
$theuser = randomuser();
$albums = $facebook->api('/' . $theuser[0] . '/albums');
foreach($albums['data'] as $album){
if ($album['type'] == 'profile'){
$photos = $facebook->api('/' . $album['id'] . '/photos');
$random_pic = $photos['data'][0]['source'];
}
}
echo "<br /><br />The User: " . $theuser[0] . "<br />";
echo "Photo Source: " . $photos['data'][0]['source'] . "<br />";
echo "THE PHOTO HEIGHT: " . $photos['data'][0]['height'] . "<br />";
echo "THE PHOTO WIDTH: " . $photos['data'][0]['width'] . "<br />";
I honestly have no idea what when in my account I can't see particular profile pictures but in my wife's I can see all of them. All help is greatly appreciated!
Cheers
Interesting case you've got there.
I can't tell you what's wrong, but I have a guess which is divided to two options.
The urls in question are for static content and so the content is not depended on the current user.
However, that url is of a cdn, which means that the copy of the content you get is different (probably) from the copy I get, which explains how is it that you're getting an error while I'm able see the picture for the same url.
The two options:
The cdn you are directed to is not available at all and so all images you ask from it will result in page not found error. Try to ping sphotos.xx.fbcdn.net and see what you get.
The cdn you are directed to does not have a copy of the image you are trying to get, maybe for some reason an error occurred when the file was supposed to propagate to the server.
This is just a guess of course, in any case I recommend you to file a bug report and have the facebook team look it up, it might take some time for them to check it out/do something about it though.