phpheadergravatar

php check if gravatar exists header error


I'm trying to check if a gravatar exists. When I try the approach recommended in earlier questions, I get an error " Warning: get_headers() [function.get-headers]: This function may only be used against URLs" Anyone seen this or see the error in my code? PS I do not want to specify a default image for gravatar to serve as there could be more than one default possibilities if no gravatar exits.

Also, I found a reference to error possibly being related to my ini file which I don't think my host gives me access to. If so, is there an alternative to getheaders? Many thanks.

$email = $_SESSION['email'];
$email= "person@gmail.com"; //for testing
$gravemail = md5( strtolower( trim( $email ) ) );
$gravsrc = "http://www.gravatar.com/avatar/".$gravemail;
$gravcheck = "http://www.gravatar.com/avatar/".$gravemail."?d=404";
$response = get_headers('$gravcheck');
echo $response;
exit;
if ($response != "404 Not Found"..or whatever based on response above){
$img = $gravsrc;
}

Solution

  • Observation

    A. get_headers('$gravcheck'); would not work because of use of single quote '

    B. calling exit; would terminate script prematurely

    C. $response would return an array you can not use echo to print the information use print_r insted

    D. $response != "404 Not Found" would not work because $response is array

    This is the proper way to do it :

    $email= "person@gmail.com"; //for testing
    $gravemail = md5( strtolower( trim( $email ) ) );
    $gravsrc = "http://www.gravatar.com/avatar/".$gravemail;
    $gravcheck = "http://www.gravatar.com/avatar/".$gravemail."?d=404";
    $response = get_headers($gravcheck);
    print_r($response);
    if ($response[0] != "HTTP/1.0 404 Not Found"){
        $img = $gravsrc;
    }