考虑以下 PHP cURL 命令:
$url = 'http://bit.ly/faV1vd';
$_h = curl_init();
curl_setopt($_h, CURLOPT_HEADER, 1);
curl_setopt($_h, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($_h, CURLOPT_HTTPGET, 1);
curl_setopt($_h, CURLOPT_URL, $url);
curl_setopt($_h, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($_h, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
$return = curl_exec($_h);
这将返回:
HTTP/1.1 301 Moved
Server: nginx
Date: Sun, 29 Apr 2012 12:48:07 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Set-Cookie: _bit=4f9d3887-00215-020af-2f1cf10a;domain=.bit.ly;expires=Fri Oct 26 12:48:07 2012;path=/; HttpOnly
Cache-control: private; max-age=90
Location: http://www.macroaxis.com/invest/market/VZ--Sat-Feb-26-06-16-35-CST-2011?utm_source=twitterfeed&utm_medium=twitter
MIME-Version: 1.0
Content-Length: 209
我想把头信息拆分成一个数组,如下
[Status] => HTTP/1.1 301 Moved,
[Server] => nginx,
[Date] => Sun, 29 Apr 2012 12:48:07 GMT,
...
[Content-Length] => 209
所以: - 第一行(HTTP/1.1 301 Moved)应该是 [Status] 的值 - 所有其他标头信息应该被拆分:
我没有成功拆分标题信息:
explode("\r\n\r\n", $return);
explode("\r\n", $return);
这不会将标头拆分为数组(:
按预期进一步拆分等。我做错了什么?