php check if gravatar exists header error


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





Loose the apostrophes around $gravcheck, otherwise it's just a string containing "$gravcheck" and not the variable's content: get_headers($gravcheck);
– Niko
May 3 '12 at 22:52


get_headers($gravcheck);





Many thanks for this catch..it was causing the error.
– user1260310
May 3 '12 at 23:15




2 Answers
2



Observation



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


get_headers('$gravcheck');


'



B. calling exit; would terminate script prematurely


exit;



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


$response


echo


print_r



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


$response != "404 Not Found"


$response



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





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
Aug 19 '14 at 18:52


strpos($response[0], "404 Not Found") === false



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()


gravatar_image()



Hope it will help to others.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API