3

how to use php get_headers only get location part?

var_dump(get_headers('http://www.google.com',1));

This return:

array(12) { [0]=> string(18) "HTTP/1.0 302 Found" ["Location"]=> string(21) "http://www.google.it/" ... }

Then I use

echo (get_headers('http://www.google.com',1)["Location"];

error: Parse error: syntax error, unexpected '[' in this echo line

fish man
  • 2,666
  • 21
  • 54
  • 94

1 Answers1

11
$headers = get_headers('http://www.google.com',1);
echo $headers["Location"];
Christian P
  • 12,032
  • 6
  • 60
  • 71
  • thanks, this no error. BTW, do you think `$headers` more quicker or `curl - curl_getinfo($ch, CURLINFO_HTTP_CODE);` more quicker – fish man Oct 29 '11 at 08:02
  • 1
    @fishman didn't try to measure it but in the http://php.net/manual/en/function.get-headers.php user damolp says that the cURL is the fastest one to obtain the headers. – Christian P Oct 29 '11 at 08:06
  • @fishman you're getting -1 for asking pointless question. both curl and get headers have to deal with network latency and none of them can speed it up. – Your Common Sense Oct 29 '11 at 08:53
  • for me lowercase worked only i.e echo $headers["location"]; – hfarazm Apr 17 '17 at 13:57