How to obtain the Array value in php?

Source: Internet
Author: User
How does php obtain the Array value. Array (& nbsp; [0] & nbsp ;=& gt; & nbsp; HTTP/1.1 & nbsp; 200 & nbsp; OK & nbsp; [1] & nbsp ;=& gt; & nbsp; how to obtain the Array value for Da php.

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


Use get_headers to obtain the Cookie value of the webpage header.
------ Solution --------------------

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

------ Solution --------------------
Get_headers (); the second parameter can use the response title as the key name.
 $ Header = get_headers ('http: // www.baidu.com/', true );
$ C = $ header ['set-cooker'];
// The response header can contain multiple Set-cookies, so they are processed in an array.
If (! Is_array ($ c) $ c = array ($ c );
Foreach ($ c as $ I ){
$ S = explode ('=', substr ($ I, 0, strpos ($ I, ';'), 2 );
$ Cookie [$ s [0] = $ s [1];
}
Var_dump ($ cookie );

Some other websites respond with set-cookie instead of Set-Cookie. So the program still has some bugs that you can implement by yourself, but I recommend you use curl

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.