Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php检查gravatar是否存在标头错误_Php_Header_Gravatar - Fatal编程技术网

php检查gravatar是否存在标头错误

php检查gravatar是否存在标头错误,php,header,gravatar,Php,Header,Gravatar,我在检查是否有墓穴。当我尝试前面问题中推荐的方法时,我得到一个错误“警告:get_headers()[function.get headers]:此函数只能用于URL”有人看到了这个或看到我代码中的错误吗?PS我不想为gravatar指定一个默认图像,因为如果没有gravatar存在,可能会有多个默认图像 另外,我发现一个错误引用可能与我的ini文件有关,我认为我的主机不允许我访问该文件。如果是的话,有没有一种替代getheaders的方法?非常感谢 $email = $_SESSION['em

我在检查是否有墓穴。当我尝试前面问题中推荐的方法时,我得到一个错误“警告:get_headers()[function.get headers]:此函数只能用于URL”有人看到了这个或看到我代码中的错误吗?PS我不想为gravatar指定一个默认图像,因为如果没有gravatar存在,可能会有多个默认图像

另外,我发现一个错误引用可能与我的ini文件有关,我认为我的主机不允许我访问该文件。如果是的话,有没有一种替代getheaders的方法?非常感谢

$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;
}
观察

A.
get_头(“$gravcheck”)

B.呼叫
退出将提前终止脚本

C.
$response
将返回一个数组,您不能使用
echo
打印信息,请使用
print\r
安装

D.
$response!=“404未找到”
将无法工作,因为
$response
是数组

这是正确的方法:

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

在做我的一个项目时,我用php制作了一个简单的Gravatar函数

你可以看到

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

  }

在$gravcheck周围去掉撇号,否则它只是一个包含“$gravcheck”的字符串,而不是变量的内容:
get_headers($gravcheck)非常感谢此捕获。这是导致错误的原因。第一个响应索引现在包含字符串“HTTP/1.1 404 Not Found”。我个人会使用
strpos($response[0],“404 Not Found”)===false
来确定报头响应是否有效,但总的来说,这是一种明确检查404的非常好的方法。