【发布时间】:2013-08-19 16:50:19
【问题描述】:
我已经为此苦苦挣扎了好几天了。
我有一个子域映射到父域中的子文件夹。 我想使用其父域中的图像图像
我可以通过在链接到文件的图像标记中使用标准 href 来很好地链接到图像。
<img href="http://www.reelfilmlocations.com/images/myimage.jpg"/>
我的问题是我需要运行一些检查以查看图像是否存在,如果不存在则显示占位符图像。
我已将所有代码放到测试页面上,图像标签可以正常显示图像,但我执行的任何调用,无论是使用 curl 还是 get_headers 都会返回 404 或 403 错误。
两个域都在同一个帐户下的同一个专用服务器上,并且它们使用相同的凭据,因此这不是安全问题(我与我的主机核实过)
那么为什么图像通过 html 显示,但我无法通过 php 获得 200 响应???
我的测试页面可以在http://mobile.reelfilmlocations.co.uk/untitled.php找到
<?php
function remoteFileExists($url) {
//$url = str_replace(" ", '%20', $url);
$agent = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15';
$curl = curl_init($url);
//don't fetch the actual page, we only want to check the connection is ok
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
//do request
$result = curl_exec($curl);
echo('curl result: '.$result.'<br>');
$ret = false;
//if request did not fail
if ($result !== false) {
//if request was ok, check response code
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
echo('curl status: '.$statusCode.'<br>');
if ($statusCode == 200 ) {
$ret = true;
}
}
curl_close($curl);
return $ret;
}
function get_response_code($theURL) {
$headers = get_headers($theURL);
foreach ($headers as $key => $value){
echo "$key => $value\n";
}
return substr($headers[0], 9, 3);
}
$image1 = 'http://www.reelfilmlocations.co.uk/uploads/images/thumbs/no-image.jpg';
$image2 = 'http://www.reelfilmlocations.co.uk/uploads/images/thumbs/Boston%20Manor%20M4-9.jpg';
$image3 = 'http://www.reelfilmlocations.co.uk/uploads/images/thumbs/thisimagedoesnotexist.jpg';
?>
IMAGE 1: <?php echo($image1); ?> (this image does exist)<br /><br />
<?php echo(remoteFileExists($image1)); ?><br />
<?php echo(get_response_code($image1)); ?><br />
<img src='<?php echo($image1); ?>'/><br /><br />
IMAGE 2: <?php echo($image1); ?> (this image does exist)<br />
<br />
<?php echo(remoteFileExists($image2)); ?><br />
<?php echo(get_response_code($image2)); ?><br />
<img src='<?php echo($image2); ?>'/><br /><br />
IMAGE 2: <?php echo($image3); ?> (this image does not exist)<br />
<br />
<?php echo(remoteFileExists($image3)); ?><br />
<?php echo(get_response_code($image3)); ?><br />
<img src='<?php echo($image3); ?>'/><br />
【问题讨论】:
-
@Dizzy Bryan High 什么是
$ret你在哪里处理这个? -
$ret 只返回真或假404...
标签: php curl http-status-code-404 get-headers