Facebook API - Nasıl (Uygulamayı "Allow" olarak kullanıcı gerektirmeden) Facebook API üzerinden bir Facebook kullanıcının profil resminizi alabilirim

10 Cevap php

Ben bir CMS kendi Facebook URL (yani, http://facebook.com/users_unique_url olan) bir kullanıcının profil resmi getirir bunun üzerinde çalışıyorum. Bunu nasıl yapabilirsiniz? Allow uygulamaya gerek kullanıcı olmadan bir kullanıcının profil resminiz URL getirir bir Faceboook API çağrısı var mı?

10 Cevap

Sadece bu URL ile veri alıp:

http://graph.facebook.com/sarfraz.anees/picture

Eğer fotoğraf almak istediğiniz kullanıcı adı ile sarfraz.anees (benim adım) değiştirin.

Bunu URL okumak ve alınan verileri işlemek için PHP'nin file_get_contents işlevini kullanabilirsiniz.

Resource:

http://developers.facebook.com/docs/api

Note: php.ini, emin OpenSSL uzantısı, bu URL'yi okumak için PHP file_get_contents işlevi kullanmak için etkin olduğundan emin olmak gerekir ise .

Göstermek için:

50x50 pixels

<img src="https://graph.facebook.com/<?= $fid ?>/picture">

200 pixels width

<img src="https://graph.facebook.com/<?= $fid ?>/picture?type=large">

To save

$img = file_get_contents('https://graph.facebook.com/'.$fid.'/picture?type=large');
$file = dirname(__file__).'/avatar/'.$fid.'.jpg';
file_put_contents($file, $img);

$ Fid kullanıcı kimliği Facebook (veya takma) olduğu ..

UPDATE:

Ağustos 2012 sonundan itibaren, API, değişik boyutlarda kullanıcının profil resimleri almak için izin için güncellendi. URL parametreleri olarak isteğe bağlı genişlik ve yükseklik alanları ekleyin:

https://graph.facebook.com/USER_ID/picture?width=WIDTH&height=HEIGHT

burada WIDTH ve HEIGHT senin talep boyut değerlerdir.

Boy oranını korumak için çalışırken bu HEIGHT WIDTH x minimum boyutu ile profil resmi dönecektir. Örneğin,

https://graph.facebook.com/redbull/picture?width=140&height=110

iadeler

    {
      "data": {
        "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/c0.19.180.142/s148x148/2624_134501175351_4831452_a.jpg",
        "width": 148,
        "height": 117,
        "is_silhouette": false
      }
   }

END UPDATE

Kullanıcının profil resmi almak için, çağrı

https://graph.facebook.com/USER_ID/picture

burada USER_ID kullanıcı kimlik numarası ya da kullanıcı adı olabilir.

Belirli bir büyüklükte bir kullanıcı profil resmi almak için, çağrı

https://graph.facebook.com/USER_ID/picture?type=SIZE

SIZE sözcüklerden biri ile değiştirilmesi gereken yerlerde

square
small
normal
large

büyüklüğüne bağlı olarak istediğiniz.

Bu çağrı seçtiğiniz tipi parametre dayalı boyutu ile tek bir görüntü için URL dönecektir.

Örneğin:

https://graph.facebook.com/USER_ID/picture?type=small

iadeler a URL to a small version of the image.

Yalnızca API profil resimlerinin değil, gerçek boyutu için maksimum boyutunu belirtir.

Kare:

50 piksel maksimum genişlik ve yükseklik.

Küçük

50 piksel ve 150 piksel maksimum yüksekliğinin maksimum genişliği.

Normal

100 piksel maksimum genişliği ve 300 piksel maksimum yükseklik.

Büyük

200 piksel maksimum genişlik ve 600 piksel maksimum yükseklik.

Eğer varsayılan USER_ID / resim ararsanız kare tip olsun.

CLARIFICATION

(Örneğin yukarıdaki başına) çağırırsanız

https://graph.facebook.com/redbull/picture?width=140&height=110

Bir JSON yanıt verecektir if you're using one of the Facebook SDKs request methods. Aksi takdirde görüntüsünün kendisini dönecektir. Her zaman, JSON almak eklemek için:

&redirect=false

şöyle:

https://graph.facebook.com/redbull/picture?width=140&height=110&redirect=false

Görüntü URL DEĞİL, ikili içeriği almak için:

$url = "http://graph.facebook.com/$fbId/picture?type=$size";

$headers = get_headers($url, 1);

if( isset($headers['Location']) )
    echo $headers['Location']; // string
else
    echo "ERROR";

Bunu yapmak için bir yolu yoktur ;)

Sayesinde "http://it.toolbox.com/wiki/index.php/Use_curl_from_PHP_-_processing_response_headers":

<?php

    /**
     * Facebook user photo downloader
     */

    class sfFacebookPhoto {

        private $useragent = 'Loximi sfFacebookPhoto PHP5 (cURL)';
        private $curl = null;
        private $response_meta_info = array();
        private $header = array(
                "Accept-Encoding: gzip,deflate",
                "Accept-Charset: utf-8;q=0.7,*;q=0.7",
                "Connection: close"
            );

        public function __construct() {
            $this->curl = curl_init();
            register_shutdown_function(array($this, 'shutdown'));
        }

        /**
         * Get the real URL for the picture to use after
         */
        public function getRealUrl($photoLink) {
            curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->header);
            curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, false);
            curl_setopt($this->curl, CURLOPT_HEADER, false);
            curl_setopt($this->curl, CURLOPT_USERAGENT, $this->useragent);
            curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
            curl_setopt($this->curl, CURLOPT_TIMEOUT, 15);
            curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
            curl_setopt($this->curl, CURLOPT_URL, $photoLink);

            //This assumes your code is into a class method, and
            //uses $this->readHeader as the callback function.
            curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, array(&$this, 'readHeader'));
            $response = curl_exec($this->curl);
            if (!curl_errno($this->curl)) {
                $info = curl_getinfo($this->curl);
                var_dump($info);
                if ($info["http_code"] == 302) {
                    $headers = $this->getHeaders();
                    if (isset($headers['fileUrl'])) {
                        return $headers['fileUrl'];
                    }
                }
            }
            return false;
        }


        /**
         * Download Facebook user photo
         *
         */
        public function download($fileName) {
            curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->header);
            curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($this->curl, CURLOPT_HEADER, false);
            curl_setopt($this->curl, CURLOPT_USERAGENT, $this->useragent);
            curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
            curl_setopt($this->curl, CURLOPT_TIMEOUT, 15);
            curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
            curl_setopt($this->curl, CURLOPT_URL, $fileName);
            $response = curl_exec($this->curl);
            $return = false;
            if (!curl_errno($this->curl)) {
                $parts = explode('.', $fileName);
                $ext = array_pop($parts);
                $return = sfConfig::get('sf_upload_dir') . '/tmp/' . uniqid('fbphoto') . '.' . $ext;
                file_put_contents($return, $response);
            }
            return $return;
        }

        /**
         * cURL callback function for reading and processing headers.
         * Override this for your needs.
         *
         * @param object $ch
         * @param string $header
         * @return integer
         */
        private function readHeader($ch, $header) {

            //Extracting example data: filename from header field Content-Disposition
            $filename = $this->extractCustomHeader('Location: ', '\n', $header);
            if ($filename) {
                $this->response_meta_info['fileUrl'] = trim($filename);
            }
            return strlen($header);
        }

        private function extractCustomHeader($start, $end, $header) {
            $pattern = '/'. $start .'(.*?)'. $end .'/';
            if (preg_match($pattern, $header, $result)) {
                return $result[1];
            }
            else {
                return false;
            }
        }

        public function getHeaders() {
            return $this->response_meta_info;
        }

        /**
         * Cleanup resources
         */
        public function shutdown() {
            if($this->curl) {
                curl_close($this->curl);
            }
        }
    }

Blog yazısı Grab the Picture of a Facebook Graph Object might offer another form of solution. Use the code in the tutorial along with the Facebook's Graph API ve PHP SDK ekledi.

Ve ... file_get_contents kullanmamaya çalışın (eğer sonuçlar yüz hazır değilseniz - bkz file_get_contents vs curl).

Ben düşünüyordum - belki ID yararlı bir araç olacaktır. Bir kullanıcı yeni bir hesap oluşturur her zaman daha yüksek bir kimliği almalısınız. Ben googled ve metadatascience.com gelen kimliği ve Mesut Seifi tarafından hesap oluşturma tarihini tahmin etmek için bir yöntem bu konuda bazı iyi veri yoktur toplanan olduğunu bulundu.

Enter görüntü açıklaması here

Bu makaleyi okuyun:

http://metadatascience.com/2013/03/11/inferring-facebook-account-creation-date-from-facebook-user-id/

Ve burada indirmek için bazı kimlikleri:

http://metadatascience.com/2013/03/14/lookup-table-for-inferring-facebook-account-creation-date-from-facebook-user-id/

Bir yolu onun cevabını kodu Gamlet posted kullanmaktır:

  • curl.php olarak kaydedin

  • Sonra dosyasında:

    require 'curl.php';
    
    $photo="https://graph.facebook.com/me/picture?access_token=" . $session['access_token'];
    $sample = new sfFacebookPhoto;
    $thephotoURL = $sample->getRealUrl($photo);
    echo $thephotoURL;
    

Ben hususlar anlamaya bana biraz zaman aldı, çünkü ben profil resimleri herkese açık olsa da, yine de onu almak için orada bir erişim belirteci olması gerekir ... Bu post düşündüm zaman {[(0) }] o.

@ NaturalBornCamper,

Güzel kodu! İşte bu süreç için bir temiz kesim kod işlevi!

function get_raw_facebook_avatar_url($uid)
{
    $array = get_headers('https://graph.facebook.com/'.$uid.'/picture?type=large', 1);
    return (isset($array['Location']) ? $array['Location'] : FALSE);
}

Bu raw Facebook avatar resmi URL'sini dönecektir. Sonra onunla istediğiniz her şeyi yapmakta özgür hissedin!

Sunucu üzerinde tam boy profil resmi kaydetmek için basit tek satır kod.

copy ("https://graph.facebook.com/FACEBOOKID/picture?width=9999&height=9999", "picture.jpg");

Openssl php.ini etkin olacak bu sadece çalışacak.