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

use*_*310 4 php gravatar header

我正在尝试检查是否存在一个gravatar.当我尝试早期问题中推荐的方法时,我收到错误"警告: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;
}
Run Code Online (Sandbox Code Playgroud)

Bab*_*aba 11

意见

A. get_headers('$gravcheck');因使用单引号而无效'

B.调用exit;会过早终止脚本

C. $response将返回一个你不能echo用来打印信息的数组使用print_rinsted

D. $response != "404 Not Found"因为$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;
}
Run Code Online (Sandbox Code Playgroud)

  • 第一个响应索引现在包含字符串"HTTP/1.1 404 Not Found".我个人会使用`strpos($ response [0],"404 Not Found")=== false`来确定标头响应是否有效,但总的来说,这是一种明确检查404的非常好的方法. (2认同)