donglang8008 2014-07-22 08:56
浏览 126

PHP:get_headers不识别url变量作为输入,但如果我使用静态字符串

I've been killing myself the last few days trying to figure out why get_headers suddenly stopped working. I checked my INI file, I tried cUrl, I did everything I could see and finally realized that something must be mysteriously wrong with the string input itself.

When I type this:

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));

Here's the output:

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

But if I take out the hardcoded URL line and just let it use the value that has been passed via AJAX to my function (comment out the two lines like so):

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));

This is what I see instead in the console:

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>

I'm sending the same URL as I post manually here, but the result is different. The type is still correct, but could the characterset be changed somehow?

Here's the ajax call:

$.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

And here's the code that creates "data" (I don't remember why I did it this way):

    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);

And of course, theURL = http://www.manatees.net/manateeb.jpg.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大