4

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= "[email protected]"; //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;
}
2
  • Loose the apostrophes around $gravcheck, otherwise it's just a string containing "$gravcheck" and not the variable's content: get_headers($gravcheck);
    – Niko
    Commented May 3, 2012 at 22:52
  • Many thanks for this catch..it was causing the error. Commented May 3, 2012 at 23:15

2 Answers 2

12

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= "[email protected]"; //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;
}
2
  • 3
    The first response index now contains the string "HTTP/1.1 404 Not Found". I would personally use strpos($response[0], "404 Not Found") === false to determine whether or not the header response is valid, but overall, this is a very nice way of explicitly checking for a 404.
    – maiorano84
    Commented Aug 19, 2014 at 18:52
  • Cool but; must be "....... if ($response[0] ......" ` $email= "[email protected]"; //for testing $gravemail = md5( strtolower( trim( $email ) ) ); $gravsrc = "gravatar.com/avatar/".$gravemail; $gravcheck = "gravatar.com/avatar/".$gravemail."?d=404"; $response = get_headers($gravcheck); if ($response[0] == "HTTP/1.1 200 OK"){ $grv_check = true; } else { $grv_check = false; }` Commented Jun 28, 2021 at 21:25
0

While doing my one of a project, I had made a simple function of Gravatar in php.

You can see it.

  <?php

  class GravatarHelper
  {

    /**
    * validate_gravatar
    *
    * Check if the email has any gravatar image or not
    *
    * @param  string $email Email of the User
    * @return boolean true, if there is an image. false otherwise
    */
    public static function validate_gravatar($email) {
      $hash = md5($email);
      $uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';
      $headers = @get_headers($uri);
      if (!preg_match("|200|", $headers[0])) {
        $has_valid_avatar = FALSE;
      } else {
        $has_valid_avatar = TRUE;
      }
      return $has_valid_avatar;
    }



    /**
    * gravatar_image
    *
    *  Get the Gravatar Image From An Email address
    *
    * @param  string $email User Email
    * @param  integer $size  size of image
    * @param  string $d     type of image if not gravatar image
    * @return string        gravatar image URL
    */
    public static function gravatar_image($email, $size=0, $d="") {
      $hash = md5($email);
      $image_url = 'http://www.gravatar.com/avatar/' . $hash. '?s='.$size.'&d='.$d;
      return $image_url;
    }

  }

In here, there are two functions.

  1. validate_gravatar() will return true or false based on if the email has any gravatar or not.
  2. gravatar_image() will return the gravatar image url for your email

Hope it will help to others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.