we have an iframe facebookapp which worked fine till yesterday. Although we didn't change anything our app suddenly stopped working.
We could identify users_isAppUser() as a problem. The method returns that the user has not added the app though he definitely has installed the app and is logged in. We could delete the try/catch part (see code below), so that the app does not get catched in a redirect loop, but the following methods do not work either:
$this->facebook->api_client->friends_get()
$this->facebook->api_client->friends_getAppUsers()
$this->facebook->api_client->call_method('facebook.users.hasAppPermission', array('ext_perm' => 'publish_stream'))
require_login() does work and we can get the facebook userid of the logged in user.
The weird thing is, that our app worked fine for a couple of weeks till yesterday.
Have there been any secret changes to the API in the last days? Or any other conclusions what the problem could be?
I would appreciate any tips. Thanks in advance!
$this->fbuserid = $this->facebook->require_login();
// check if user has added app, exception gets thrown if the cookie has an invalid session_key i.e. user is not logged in
try {
if(!$this->facebook->api_client->users_isAppUser()) {
$this->facebook->redirect($this->facebook->get_add_url());
}
} catch (exception $ex) {
// clear cookies for application and redirect to login prompt
$this->facebook->set_user(null, null);
$this->facebook->redirect($this->configArray['appcallbackurl']);
}
<?php
// this is sample code taken from the Facebook Developers Site.Thank you to Face book
define('YOUR_APP_ID', '');
define('YOUR_APP_SECRET', '');
function get_facebook_cookie($app_id, $app_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $app_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET);
echo "<pre/>";
/*print_r($_COOKIE);
print_r($cookie);*/
$user = json_decode(file_get_contents('https://graph.facebook.com/me?access_token=' . $cookie['access_token']));
$photo = json_decode(file_get_contents('https://graph.facebook.com/100000439661780/albums?access_token=' . $cookie['access_token']));
echo "<pre/>";
print_r($photo);
?>
<img src="https://graph.facebook.com/ureshpatel5/picture" height="200" width="200" />
<html>
<body>
<?php
$albums = $facebook->api('/me/albums');
print_r($albums);
foreach($albums['data'] as $album)
{
// get all photos for album
$photos = $facebook->api("/{189346844423303_46487}/photos");
foreach($photos['data'] as $photo)
{
echo "<img src='{$photo['source']}' />", "<br />";
}
}
?>
<?php if ($cookie) { ?>
Welcome <?= $user->name ?>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<?php
echo $photo->source;
?>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '<?= YOUR_APP_ID ?>', status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
</script>
</body>
</html>
Related
I've been trying to figure this out for like 3 hours and am not getting anywhere. I'm trying to set the img src in the avatar div to the Roblox user's avatar URL with this endpoint https://thumbnails.roblox.com/v1/users/avatar-bust?userIds=3102777127&size=150x150&format=Png&isCircular=true but it requires usernames and my data is usernames so we have to use this endpoint to get IDs https://api.roblox.com/users/get-by-username?username=VOICECHAT22983. I have been trying a bunch of different stuff and nothing is working and just getting a bunch of errors.
This is how the data looks:
brenda12322323
eunaodoaisnudhnac
Lovesroblox1334
Cho5963
Sinmin191
vikaa_qwixxk
yourfav_stepsister
SnipesG0D
This is the PHP Code
<?php
$lines = file('../txt/names.txt');
foreach ($lines as $line) {
echo '<div class="avatar">
<img src="'.$avatar.'" alt="">
<h1>' . $line . '</h1>
Profile
</div>';
}
?>
I'm trying to set the $id variable as the user's ID and the avatar variable as the image URL
You need to get all the IDs first from the get-by-username endpoint, pass those into the avatars endpoint, then put the data back together:
<?php
/*
Question Author: cdnAshton
Question Answerer: Jacob Mulquin
Question: PHP Set Roblox Avatar to Image
URL: https://stackoverflow.com/questions/74977675/php-set-roblox-avatar-to-image
Tags: php, roblox
*/
function get_id_by_username($name)
{
$url = 'https://api.roblox.com/users/get-by-username?username=' . $name;
echo 'getting ' . $url . PHP_EOL;
return json_decode(file_get_contents($url), true);
}
function get_avatar_urls($ids)
{
$ids_param = implode(',', $ids);
$url = 'https://thumbnails.roblox.com/v1/users/avatar-bust?userIds='.$ids_param.'&size=150x150&format=Png&isCircular=true';
return json_decode(file_get_contents($url), true);
}
if (!file_exists('users.json')) {
$names = file('names.txt');
$users = [];
foreach ($names as $name) {
$name = trim($name);
$id = get_id_by_username($name);
$users[$id['Id']] = $id;
sleep(15); // Needed otherwise you will hit API rate-limits
}
file_put_contents('users.json', json_encode($ids, JSON_PRETTY_PRINT));
} else {
$users = json_decode(file_get_contents('users.json'), true);
$ids = [];
foreach ($users as $user) {
$ids[] = $user['Id'];
}
$avatars = get_avatar_urls($ids);
foreach ($avatars['data'] as $avatar) {
$users[$avatar['targetId']]['AvatarUri'] = $avatar['imageUrl'];
}
file_put_contents('users.json', json_encode($users, JSON_PRETTY_PRINT));
}
The script is executed twice, the first time it will read the names from names.txt, and get the IDS, saving into a users.json file. Then when you execute it again, it will get the IDs from the JSON file, then call the avatars and then merge it back together. The result is a JSON file like so:
{
"3528175625": {
"Id": 3528175625,
"Username": "brenda12322323",
"AvatarUri": "https:\/\/tr.rbxcdn.com\/3c195f46b44d37aa250c0d7c6ae41ded\/150\/150\/AvatarBust\/Png\/isCircular",
"AvatarFinal": false,
"IsOnline": false
},
"4028593775": {
"Id": 4028593775,
"Username": "eunaodoaisnudhnac",
"AvatarUri": "https:\/\/tr.rbxcdn.com\/0fc96af99739e29c780af459db6c2bd8\/150\/150\/AvatarBust\/Png\/isCircular",
"AvatarFinal": false,
"IsOnline": false
}
}
Then to output this information on a webpage:
$users = json_decode(file_get_contents('users.json'), true);
foreach ($users as $user) {
echo '<div class="avatar">
<img src="'.$user['AvatarUri'].'" alt="">
<h1>' . $user['Username'] . '</h1>
Profile
</div>';
}
Gives you something like this:
Problem:
So when I login, a user can go to the browse.php to add images to the favorites.php, and the photos will appear on that page.
When I log out and log back in, the images are gone (not saved).
Question:
How do I save it after I logout so on next login I get to see the favorite'd images from that particular user using cookies or other methods (can't use database because I'm not allowed to)?
My favorites page
<?php
session_start();
require_once 'favoritesfunction.php';
if (isset($_GET["fileName"])) {
$fileName = $_GET["fileName"];
addToFavorites($fileName);
}
?>
<!DOCTYPE html>
<html>
<head>
<?php
$title = "Travel Photos - View Favorites";
?>
<link rel="stylesheet" href="viewfavorites.css" />
</head>
<body>
<main class="container">
<h1 class="favheader">Favorites</h1>
<div class="content">
<?php
if (isset($_SESSION['favorites']) && isset($_SESSION['email'])) {
$favorites = $_SESSION['favorites'];
if (count($favorites) > 0) {
?> <ul class="ul-favorite"> <?php
foreach ($favorites as $f) {
echo '<img class="displayPic" src="https://storage.googleapis.com/assignment1_web2/square150/' . $f . '">';
}
?>
</ul>
<p id="removeall"><button class="button"><span><a target="" href="services/removefavorites.php?remove=all">Remove All</a></span></button></p>
<?php
}
} else {
?>
<div class="nofavorites">
<p>No favorites found.</p>
</div>
<?php
}
?>
</div>
</main>
</body>
<script type="text/javascript" src="main.js"></script>
</html>
My add to favorites function php
<?php
function addToFavorites($fileName)
{
//Checks if the session favorites exists
if (isset($_SESSION['favorites'])) {
$favorites = $_SESSION['favorites'];
} else {
$favorites = array();
$_SESSION['favorites'] = $favorites;
}
// add item to favourites
$item = $fileName;
$match = false;
//Loop below checks for duplicates
$i = 0;
while ($i < count($favorites)) {
if ($favorites[$i] == $item) {
$favorites[$i] = $item;
$match = true;
}
$i++;
}
//if match equals false, that means its not in the favorites list of the user
//so it is added to the user's favorites array
if ($match == false) {
$favorites[] = $item;
}
$_SESSION['favorites'] = $favorites;
$_SESSION['favorites'] = $favorites;
}
My approach:
I tried doing something like setting $name = $_SESSION['email'] and $value="<img src=...>" then setcookie($name, $value) and then if(isset($_COOKIE[$value])) echo $value
I know this is totally wrong, I tried looking everywhere online to try to solve this, if I could get some help that would be great thanks!
I'm trying to make a likes/claps/heart system on my site and found this site (https://www.techolac.com/wordpress/how-to-add-likes-to-posts-in-wordpress-without-a-plugin/) that taught how to do it. I made some adjustments the problem is that when I do like the page refresh and I just wanted the number to refresh and not the whole page.
I saw what I could do in AJAX but i didnt know how.
Functions.php / Wordpress
// Add buttons to top of post content
function ip_post_likes($content) {
ob_start();
?>
<a href="<?php echo add_query_arg('post_action', 'like'); ?>">
<span class="icon-claps pr-2"><?php echo get_like_count('likes') ?></span>
</a>
<?php
$output = ob_get_clean();
return $output . $content;
}
add_filter('the_content', 'ip_post_likes');
//Get like
function get_like_count($type = 'likes') {
$current_count = get_post_meta(get_the_id(), $type, true);
return ($current_count ? $current_count : '');
}
//Process like
function ip_process_like() {
$processed_like = false;
$redirect = false;
// Check if like
if(is_singular('post')) {
if(isset($_GET['post_action'])) {
if($_GET['post_action'] == 'like') {
// Like
$like_count = get_post_meta(get_the_id(), 'likes', true);
if($like_count) {
$like_count = $like_count + 1;
}else {
$like_count = 0;
}
$processed_like = update_post_meta(get_the_id(), 'likes', $like_count);
}
if($processed_like) {
$redirect = get_the_permalink();
}
}
}
// Redirect
if($redirect) {
wp_redirect($redirect);
die;
}
}
add_action('template_redirect', 'ip_process_like');
The problem is that when I do like the page refresh and I just wanted the number to refresh and not the whole page.
I saw what I could do in AJAX but i didnt know how.
Image:
This is the whole code. I am using wordpres and twig / timber
I am trying to integrate a Tumblr blog into my website, using the TumblrPHP wrapper available at https://github.com/gregavola/tumblrPHP. This is code I'm using to view the posts on my site:
<?php
include ('lib/tumblrPHP.php');
$consumer = 'key';
$secret = 'key';
$tumblr = new Tumblr($consumer, $secret);
$posts = $tumblr->get('/blog/nyhetergaius.tumblr.com/posts');
foreach($posts->response->posts as $posts) {
?>
<h2><?php echo date('Y-m-d', $post->timestamp) ?></h2>
<?php if ($post['type'] == 'regular') { ?>
<?php echo $post{'body'}; ?>
<?php } ?>
<?php
if ($post->type == 'photo') {
foreach ($post->photos as $photo) {
?>
<img src="<?php echo $photo->alt_sizes[1]->url ?>" />
<?php
}
echo $post->caption;
}
else
echo $post->body;
?>
<?php
}
?>
I am using my own key and secret key, but even so I can only retrieve a default date for each post. What am I doing wrong? Is there an easier way to view the posts as they appear on the blog http://nyhetergaius.tumblr.com/? This is how the posts appear on my site: http://test.gaius.nu/om.php.
Thank you for your support.
I was able to make this work by replacing $post['type'] with $post->type etc..
foreach($posts->response->posts as $posts) {
$postDate = date('Y-m-d', $posts->timestamp);
echo "<h2>$postDate</h2>";
if ($posts->type == 'regular') {
echo $posts->body;
}
elseif ($posts->type == 'photo') {
foreach ($posts->photos as $photo) {
$imgURL = $photo->alt_sizes[1]->url;
echo "<img src=\"$imgURL\" />";
}
echo $posts->caption;
}
else
{
echo $posts->body;
}
}
I just tried using the example given on developers.facebook.com to open a request form and invite friends, so I changed everything accordingly but nothing happens, do I need to launch it in some way?
<?php
$api_key = '...';
$secret = '...';
require_once 'facebook.php';
// Names and links
$app_name = "...";
$app_url = "http://apps.facebook.com/.../"; // Assumes application is at http://apps.facebook.com/app-url/
$invite_href = "invite.php"; // Rename this as needed
$facebook->require_frame();
$user2 = $facebook->require_login();
if(isset($_POST["ids"])) {
echo "<center>Thank you for inviting ".sizeof($_POST["ids"])." of your friends on <b>".$app_name."</b>.<br><br>\n";
echo "<h2>Click here to return to ".$app_name.".</h2></center>";
} else {
// Retrieve array of friends who've already authorized the app.
$fql = 'SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1='.$user2.') AND is_app_user = 1';
$_friends = $facebook->api_client->fql_query($fql);
// Extract the user ID's returned in the FQL request into a new array.
$friends = array();
if (is_array($_friends) && count($_friends)) {
foreach ($_friends as $friend) {
$friends[] = $friend['uid'];
}
}
// Convert the array of friends into a comma-delimeted string.
$friends = implode(',', $friends);
// Prepare the invitation text that all invited users will receive.
$content =
"<fb:name uid=\"".$user2."\" firstnameonly=\"true\" shownetwork=\"false\"/> has started using ".$app_name." and thought it's so cool even you should try it out!\n".
"<fb:req-choice url=\"".$facebook->get_add_url()."\" label=\"Put ".$app_name." on your profile\"/>";
?>
<fb:request-form
action="<? echo $invite_href; ?>"
method="post"
type="<? echo $app_name; ?>"
content="<? echo htmlentities($content,ENT_COMPAT,'UTF-8'); ?>">
<fb:multi-friend-selector
actiontext="Here are your friends who don't have <? echo $app_name; ?> yet. Invite whoever you want -it's free!"
exclude_ids="<? echo $friends; ?>" />
</fb:request-form>
<?php
}
?>
hello the following code works for me
<fb:serverFbml width="760px">
<script type="text/fbml">
<fb:fbml>
<fb:request-form
action='http://apps.facebook.com/yoursite/'
method='POST'
type='photo fun'
content="Let's fun with facebook
<fb:req-choice url='http://www.yoursite.com' label='Register'/>"
<fb:multi-friend-selector actiontext="Select your friends."></fb:multi-friend-selector>
</fb:request-form>
</fb:fbml>
</script>
</fb:serverFbml>