php 怎么取Array值

WBOY
Release: 2016-06-13 12:56:15
Original
911 people have browsed it

php 如何取Array值。

<br />
Array<br />
(<br />
    [0] => HTTP/1.1 200 OK<br />
    [1] => Date: Mon, 24 Dec 2012 16:15:18 GMT<br />
    [2] => Server: BWS/1.0<br />
    [3] => Content-Length: 9888<br />
    [4] => Content-Type: text/html;charset=gbk<br />
    [5] => Cache-Control: private<br />
    [6] => Expires: Mon, 24 Dec 2012 16:15:18 GMT<br />
    [7] => Set-Cookie: BAIDUID=9AFFEA9B11EC8D358FC1A13A8DCC83F9:FG=1; expires=Mon, 24-Dec-42 16:15:18 GMT; path=/; domain=.baidu.com<br />
    [8] => P3P: CP=" OTI DSP COR IVA OUR IND COM "<br />
    [9] => Connection: Close<br />
)<br />
Copy after login


用get_headers,如何取到网页头Cookie值。


------解决方案--------------------
<br />
$array = array('Set-Cookie: BAIDUID=9AFFEA9B11EC8D358FC1A13A8DCC83F9:FG=1; expires=Mon, 24-Dec-42 16:15:18 GMT; path=/; domain=.baidu.com');<br />
foreach ($array as $value) {<br />
    if (stripos($value, 'Set-Cookie') !== false)<br />
        echo $value;<br />
}<br />
Copy after login

------解决方案--------------------
get_headers(); 第二个参数可以让响应标题作为键名
<?php<br />
$header = get_headers('http://www.baidu.com/', true);<br />
$c = $header['Set-Cookie'];<br />
// 响应头里可以包含多个 Set-Cookie,所以统一为数组处理<br />
if( ! is_array($c) ) $c = array($c);<br />
foreach($c as $i){<br />
    $s = explode('=', substr($i, 0, strpos($i, ';')), 2);<br />
    $cookie[$s[0]] = $s[1];<br />
}<br />
var_dump( $cookie);
Copy after login

还有一些网站是以 set-cookie 响应,而不是Set-Cookie 这样的。所以程序还是有一些Bug,你可以自己去实现,不过我更推荐你使用 curl

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template