过去几天我一直在自杀,试图弄清楚为什么 get_headers 突然停止工作。我检查了我的 INI 文件,我尝试了 cUrl,我做了我能看到的一切,最后意识到字符串输入本身一定有一些神秘的错误。
当我输入这个:
if (filter_var($url, FILTER_VALIDATE_URL)!== false)
die(json_encode(array('error' => "Your image link doesn't seem to be valid. Are you sure you copied the whole URL?")));
echo gettype($url).'::::';
$url = 'http://www.manatees.net/manateeb.jpg';
echo $url;
echo gettype($url);
print_r(get_headers($url,1));
这是输出:
string::::http://www.manatees.net/manateeb.jpgstringArray
(
[0] => HTTP/1.1 200 OK
[Date] => Tue, 22 Jul 2014 08:40:43 GMT
[Server] => Apache/2.2.22 (Debian)
.... and so on
但是,如果我取出硬编码的 URL 行并让它使用通过 AJAX 传递给我的函数的值(像这样注释掉这两行):
if (filter_var($url, FILTER_VALIDATE_URL)!== false)
die(json_encode(array('error' => "Your image link doesn't seem to be valid. Are you sure you copied the whole URL?")));
//echo gettype($url).'::::';
//$url = 'http://www.manatees.net/manateeb.jpg';
echo $url;
echo gettype($url);
print_r(get_headers($url,1));
这是我在控制台中看到的:
http//www.manatees.net/manateeb.jpgstring<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: get_headers(): This function may only be used against URLs in E:\Stuff\Working\htdocs\image_upload.php on line <i>180</i></th></tr>
我发送的 URL 与我在此处手动发布的 URL 相同,但结果不同。类型仍然正确,但是字符集可以以某种方式更改吗?
这是ajax调用:
$.ajax({
beforeSend: spinnerOn(),
url: 'image_upload.php',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
这是创建“数据”的代码(我不记得我为什么这样做了):
var data = new FormData(); // create formdata (HTML5 object for uploading) object
data.append('action', 'upload'); // Depending on why the image was uploaded, perform different actions
data.append('url', theURL);
当然,URL = http://www.manatees.net/manateeb.jpg。