How to check if the Graph API of Facebook is returning an image or not?

0

I'm working on a project that uses the user photo as the facebook profile photo, but some users do not have facebook profile, so the Graph API returns error and does not return an image, which function can I use to check if does the Graph API url return an image or not?

    
asked by anonymous 24.06.2015 / 21:21

1 answer

1

Gustavo , I have worked with OpenGraph, here's an example I made:

<?php

$user_id = 'SociedadeFemininaOficial';
//$user_id = 'UsuarioErrado';

try
{
    $user_photo = @file_get_contents("https://graph.facebook.com/v2.2/{$user_id}/picture?redirect=false");

    if(strpos($http_response_header[0], "200") === FALSE)
    {
        // usuário não contém foto ou não deu permissão para tal
        echo "Usuário não contém foto ou não deu permissão para tal";
    }
    else
    {
        $user_photo = json_decode($user_photo);
        echo $user_photo->data->url;
    }
}
catch (Exception $ex)
{
    throw new Exception($ex);
}

Note: Instead of file_get_contents you can also use cURL to receive the data. Just check the status of sweet http.

    
25.06.2015 / 02:31