How can I account for headers that don't follow proper capitalization?

279 views Asked by At

Updated Question:

When using get_headers() with the format option to use named array keys (rather than numbered keys), how can I account for situations like the server using Content-type rather than Content-Type?


Original Question: PHP problem getting header of a file that has a colon in url
Hi, I am having trouble retrieving header information of a remote url which has a colon in it. I have tried using urldecode and it decodes %3A into :, however it does not solve the problem. What to do?
This url: http://string-theory.wdfiles.com/local--files/char%3Ajezebel/evangeline-lilly-picture-4a.jpg

1

There are 1 answers

7
drudge On

Updated Answer:

If you can't guarantee that the server uses correct capitalization, you can use array_change_key_case to change the named keys.

<?php
    $original = array('Content-type'=>'text/html');
    echo $original['Content-Type']; // Notice: Undefined index

    $fixed = array_change_key_case($original, CASE_LOWER);
    echo $fixed['content-type']; // prints 'text/html'
?>


Old Answer:

You're going to have to expand on your question, because this works just perfectly:

PHP:

<?php
    $url = 'http://www.mysite.com/dev/blah.php?blah=1:2:3';
    print_r(get_headers($url));
?>

Output:

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Date: Tue, 22 Feb 2011 18:28:20 GMT
    [2] => Server: Apache
    [3] => Connection: close
    [4] => Content-Type: text/html
)

Output 2: (using URL given by OP)

Array
(
    [0] => HTTP/1.0 200 OK
    [1] => Etag: "8b48b69ed24101b9a235e0168874c720"
    [2] => Content-type: image/jpeg; charset=utf-8
    [3] => Content-Length: 482111
    [4] => Connection: close
    [5] => Date: Wed, 23 Feb 2011 19:50:42 GMT
    [6] => Server: lighttpd/wikidot
)