0
function getUserFriends(a) {
  var id=a;
  FB.api('/me/friends?fields=picture', function (response) {
    //console.log('Got friends: ', id);
    var x = y = 0;
    var photos=[];
    for (var i=0;i < response.data.length;i++) {
            photos = response.data[i].picture.data.url;
            $.post("insert_photo.php", {
            "pic": photos,
            "id":id
        }, function (data) {

        });

    }
}

I am using a Facebook API to call my friends friends profile pic and using a post inserting into the db , suppose if i have 500 friends , it will send the post request 500 times , now i want to store it in the array and then the post request . is it possible , if yes please tell me how

Sahil Mittal
  • 20,351
  • 12
  • 59
  • 88
PHP_USER1
  • 174
  • 1
  • 11

2 Answers2

0

Getting Friends

v2.0 onwards, getting the list of friends with /me/friends has been removed. So, the method you are using will be removed in a few months.

But there is another option now to get the friends (basically the taggable friends) with end point /me/taggable_friends; but this needs to be approved first. (Check out the complete discussion)

Save profile pictures

If you want to save the urls in an array and then make the post requests, you can do that too-

FB.api('/me/taggable_friends', function (response) {
   var x = y = 0;
   var photos=[];
   for (var i=0;i < response.data.length;i++) {
        var user={};
        user.photo = response.data[i].picture.data.url;
        user.id = response.data[i].id;
        photos.push(user);
   }

   // at this point all the friends data have been saved in the array
   for(var i=0; i<photos.length;i++){
       $.post("insert_photo.php", {
            "pic": photos[i].photo,
            "id":photos[i].id
          }, function (data) {
             // done
       });
   }
});

Or the cleaner way-

Send the photos array to the php script and let php script loop through it and save to the db-

 $.post("insert_photo.php", {
        "data": JSON.stringify(photos)
      }, function (data) {
         // done
 });
Community
  • 1
  • 1
Sahil Mittal
  • 20,351
  • 12
  • 59
  • 88
0

I am not sure if I understood you, but if you want to do only one POST call, you could move the $.post() outside of the for loop. And then send all the photos array as json.

For example:

function getUserFriends(a) {
  var id=a;
  FB.api('/me/friends?fields=picture', function (response) {
    //console.log('Got friends: ', id);
    var x = y = 0;
    // Storing all photo urls in array
    var photos=[];
    for (var i=0;i < response.data.length;i++) {
        photos.push(response.data[i].picture.data.url);
    }

    // Send photos as JSON
    $.post("insert_photo.php", {
            "pics": photos,
            "id":id
       }, function (data) {}
    );
}

This will send all the photos in a single POST call. You will have to update the insert_photo.php script to iterate over the "pics" param. Something like:

<?php
[...]
foreach (json_decode($_POST['pics']) as $pic) {
     [...]
     // You can use $pic here
     [...]
}
[...]
aaronfc
  • 118
  • 5