Download Multiple Profile Photos

This is what i am doing right now.

 foreach($response['data'] as $friends_phtoos) {
     $f_id = $friends_phtoos['id'];
     $img = 'https://graph.facebook.com/'.$f_id.'/picture';
     $image = $f_id.'.jpg';
getFacebookPhoto($img, $image);
 }

I am doing this loop for maximum 10 photos However it is taking lot of time to download all the 10 photos.

What is the best way to get those photos quickly? I have heard about batch. But I don't know how to use it with PHP.

getFacebookPhoto function is a curl function to download photos. Can I use curl to get Facebook photos? Is it against FB's terms and condition? (additional question ;)

Answers


You're making 10 API calls which is slowing you down. There is no problem with using curl, but since I don't know what your getFacebookPhoto function looks like I'm going to use file_get_contents to give you a complete solution.

The trick is to make one API call and get all the photos. Using the Graph API path /ID/photo/ also slows things down a little as this results in a 301 redirect to the user's photo. I'm going to use FQL since it's a lot quicker. This gives you an array with IDs and URLs to the Facebook photo. Don't assume that the data will come back in the same order you send the IDs.

$f_id = array();
foreach($response['data'] as $friends_phtoos) {
     $f_id[] = $friends_phtoos['id'];
 }
$ids = implode(',',$f_id);
$fql = "SELECT id, pic FROM profile WHERE id IN ({$ids})";
$url = 'http://graph.facebook.com/fql?q=' . urlencode($fql);
$fb_photos = json_decode( file_get_contents($url) );

Considering there is nothing special about this forloop, the problem most likely lies with your download. The reason this is taking so long has nothing to do with your implementation but most likely with the download speed itself.


Need Your Help

How to modify PATH for non-interactive SSH call in RHEL 5?

unix ssh redhat rhel5

I am trying to modify the PATH variable of my SSH server such at a non-interactive shell command ssh myserver.com 'echo $PATH' returns the desired path. I tried modifying ~/.bashrc and ~/.profile f...