Reputation: 2227
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;
}
Upvotes: 4
Views: 2831
Reputation: 5025
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.
validate_gravatar()
will return true or false based on if the email has any gravatar or not.gravatar_image()
will return the gravatar image url for your emailHope it will help to others.
Upvotes: 0
Reputation: 95103
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;
}
Upvotes: 12