facebook api get my friends profile pic - php

Hi there I am creating an app and I want to display my friends profile picture, but what happens in my case is that my profile picture is displayed x the number of the friends that I have. Any idea of what I should change on my code ? Thanks.
$uid = $facebook->$facebook->api('/me/friends/');
//create the url
$profile_pic = "http://graph.facebook.com/".$uid."/picture";
foreach($user_graph['data'] as $photokey => $photovalue){
echo "<img src=\"" . $profile_pic . "\" />";
}

From the code you have provided I see a couple of problems. Why are you calling $facebook->$facebook->api()? Surely that should be $facebook->api('/me/friends/');
Secondly, your api call returns an array, it looks like you are trying to treat $uid as a single value to me.
Also, If your loop works I imagine $user_graph must be defined elsewhere in your code.
I would probably just do something like this:
$friendlist = $facebook->api('/me/friends/');
foreach($friendlist["data"] as $value) {
echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
}
It's not going to look very neat but it should yield the result you were originally going for.

Related

Im new to php, how can I use links to change what loads on a page?

What I was looking for before was a way to dynamically create links for the same directory but have different URL variables, that way the same page works from the variables provided to load images from a folder. Below was my example code for loading all of the images inside the directory.
$dir = "./Ch.001/";
$a = glob($dir."*png");
foreach($a as $image){
echo "<img src='". $image ."'>";
}
?>
At the time I wasn't sure what to research to achieve what I wanted but thanks to an answer and some more experience in PHP, my answer to what I was looking for above would have been $_GET variables and urlencode()/htmlspecialchars().
Now I would use a database to store all of the information I wanted to display, so when the series is created from a form I would store the title, description, etc, and then calculate how many rows are in the database, and then make that number + 1 the file name, and maybe add a prefix so it isn't just numbers.
Presuming 001 is 001 to n etc.
Use a simple $_GET parameter, check or cast to int, 0 pad it and then show your links with +1 for next.
<?php
$chapter = str_pad((int) ($_GET['chapter'] ?? '001'), 3, "0", STR_PAD_LEFT);
//
$dir = "./Ch.$chapter/";
//
$images = glob($dir."*png");
if (empty($images)) {
echo 'Go back';
} else {
foreach($images as $image){
echo "<img src='". $image ."'>";
}
echo 'Chapter: '.$chapter.PHP_EOL;
echo 'Goto Chapter';
}

Print an array value when its key matches with a variable (PHP)

I have a "album.php" page with four albums; when you click on one of them it links to "gallery.php" showing the photos of that specific album.
Now I would like to put a specific title in "gallery.php" depending on the album clicked by the user and I would like to do it in a nice and clean way.
gallery.php:
$query_url = $_SERVER['QUERY_STRING'];
$pageID = substr($query_url, 6);
$album_title = array("album1" => "title1",
"album2" => "title2",
"album3" => "title3");
while($album_title[KEY] = $pageID)
{
echo '<div id="album_title">
<h2 class="body_text">'.$album_title[VALUE].'</h2>
</div>';
}
I thought something like that instead of the less dynamic:
if($pageID = "album1")
{echo "title1";}
if($pageID = "album2")
{echo "title2";}
etc.
My problem is that I don't know how I could "say":
when(array[KEY] = $pageID)
{
echo 'the correspondent array[VALUE]';
}
Thanks everyone!
[EDIT] Voting negatively without an explication doesn't solve any problem in this world, mate. Was probably 'cause I used "while" instead of "foreach" in my example? I know about its existence but I tried multiple times with it also without success.
Provinding $pageID is something like "album1" you wouldn't need any loop:
echo '<div id="album_title">
<h2 class="body_text">' . $album_title[$pageID] . '</h2>
</div>';
Check it out.

Display single latest instagram photo on your website

I am looking all over the web and can't believe I can't find an answer to my question.
I would like to display the latest single instagram photo on my website. simple as that.
No gallery, no fancybox, no slideshow etc etc
I found this useful link:
http://www.blueprintinteractive.com/blog/how-instagram-api-fancybox-simplified
it's actually working but it doesn't give an option of how many images you would like to display
and I would like only one latest instagram image.
anyone have an idea?
Thanks
You can break foreach after showing first image like below:
foreach ($result->data as $post) {
// Do something with this data.
break; // for one result only
}
Or add &count=1 at end of url as below
https://api.instagram.com/v1/users/ID-GOES-HERE/media/recent/?access_token=TOKEN-GOES-HERE&count=1
The answer is right here: http://instagram.com/developer/endpoints/users/#get_users_media_recent_with_client_id
Count option for number of media !
Try this:
$page_id = 'YOUR ID';
$access_token = 'YOUR ACCESS TOKEN';
$json_object = #file_get_contents('https://api.instagram.com/v1/users/' . $page_id .
'/media/recent/?access_token=' . $access_token . '&count=1');
$indata = json_decode($json_object);
echo $indata->data[0]->images->low_resolution->url // for low res
echo $indata->data[0]->images->thumbnail->url; //for thumbnail
echo $indata->data[0]->images->standard_resolution->url; //for standard res
echo $indata->data[0]->caption->text; //for caption
Embedding the whole post may be a solution:
$json_post = #file_get_contents('https://api.instagram.com/oembed/?url=' . $indata->data[0]->link);
$oembed = json_decode($json_post, true);
echo $oembed['html'];
Good luck!

build link using postgresql and php

)
I have a question:
I have a databse with the following information:
Name of report [name]
Link to report [link]
The various reports are displayed in tables in my page (using db_tables filed with static, month, week etc)
But i want to generate a link to the report based on "link" and "name"
so i set up a pgsql query :
result = select name, link from db where type = 'week'
This works fine.
To display the above i have:
echo "<div id=\"selMaand\">" ;
echo "<table id = \"t2\">\n ";
echo "\t<tr class=\"head\">\n";
echo "<th colspan=\"2\">Select Maand</th>";
while ($line = pg_fetch_array($result5, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td width=\"100%\">$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
echo "</div>";
which works fine to.
Now, the link from database is http://192.168.178.1:8080/etc/etc/page.html"onclick humptydumpty>name
each time i want to adjust something in either the onlclick or address, i have to change all my links.
What can i insert in my querys so i will be able to build a link like;
echo "<a href = "http://192.168.178.1:8080/etc/etc/**$link**"onlclick humptydumpyu>**$name**</a>
I cant figure out how to break down my array!
Thanks in advance
Sjoerd
If you just need a portion of your URL in the database (i.e. only a portion of it would change for each record), then only store that portion of the URL in the database. You can then generate the remaining portion of the URL (call it the URL base) in your code.
So something like this:
define('URL_BASE', 'http://some.url.base/that/you/can/change/globally/');
Then you get the remaining piece of the URL from the database for each item. So you would have something like this inside your query loop to build the full URL.
$link = $line['link'];
$final_url = URL_BASE . $link;
As far as the onclick stuff goes, I would have that set sepearately as well, since I assume the onclick behavior is not dependent on the individual link (if it is you can make a separate DB column for it).
define('LINK_ONCLICK', 'onclick="some_onclick_function()"');
And in your query loop you can put this together like this:
$name = $line['name'];
$link = $line['link'];
$final_url = URL_BASE . $link;
echo '<a href="' . $final_url . '" ' . LINK_ONCLICK . '>' . $name . '</a>';
Note that it is not really necessary to define the strings for the url base and onclick behavior as constants. I just showed it this way as it is common practice to set globally defined values that do not need to change at run-time ion such a manner.

how to get user's random friend in facebook application?

I have a friendlist that displays all of user's facebook friends.
What I actually want is to display only one friend at a time, selected randomly.
is there any way to do so?
<?
foreach ($friends as $friend)
{
echo "<img src='http://graph.facebook.com/" . $friend['id'] ."/picture'><br>";
}
?>
<?
$friend_count = count($friends);
$random_friend_num = rand(0,$friend_count-1);
$random_friend = $friends[$random_friend_num];
echo "<img src='http://graph.facebook.com/" . $random_friend['id'] ."/picture'><br>";
?>
The code is pretty much self explanatory..
Get Friend count
get Random number in range 0 to friendCount
paste Random friend
If my answer is what you needed, feel free to accept it :)

Categories