Skip to content
This repository has been archived by the owner on Mar 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #28 from nurikabe/master
Browse files Browse the repository at this point in the history
Don't crash if there is a DNS hiccup.
  • Loading branch information
henrikbjorn committed Jan 31, 2014
2 parents a3cd434 + db11654 commit 733ccdd
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions GravatarApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,23 @@ public function getUrlForHash($hash, $size = null, $rating = null, $default = nu
}

/**
* Checks if a gravatar exists for the email. It does this by checking for 404 Not Found in the
* body returned.
* Checks if a gravatar exists for the email. It does this by checking for the presence of 404 in the header
* returned. Will return null if fsockopen fails, for example when the hostname cannot be resolved.
*
* @param string $email
* @return Boolean
* @return Boolean|null Boolean if we could connect, null if no connection to gravatar.com
*/
public function exists($email)
{
$path = $this->getUrl($email, null, null, '404');

$sock = fsockopen('gravatar.com', 80, $errorNo, $error);
fputs($sock, "HEAD " . $path . " HTTP/1.0\r\n\r\n");
if (!$sock = @fsockopen('gravatar.com', 80, $errorNo, $error)) {
return null;
}

fputs($sock, "HEAD " . $path . " HTTP/1.0\r\n\r\n");
$header = fgets($sock, 128);

fclose($sock);

return strpos($header, '404') ? false : true;
}
}

0 comments on commit 733ccdd

Please sign in to comment.