How to check whether or not Facebook’s Graph API is returning an image?

Asked

Viewed 214 times

0

I’m working on a project that uses the user photo as the facebook profile photo, but some users have no facebook profile, so the Graph API returns error and does not return an image, what function can I use to check whether or not the Graph API url is returning an image?

1 answer

1

Gustavo, I have worked with Opengraph, below is an example I did:

<?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);
}

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.