note 92580 added to function.curl-exec
| From: | petermiller1986 | Date: | Thu, 30 Jul 2009 08:57:16 +0000 |
| Subject: | note 92580 added to function.curl-exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158902@lists.php.net to get a copy of this message | ||
so far i have not come across any code or library file that will
extract cookie information from a http header, so i've written
one.
there are two files:
file1.php - acts as server and sets the cookies
file2.php - acts as browser and retrieves the cookies set by file1
/*****file1.php****/
<?php
/*set 3 cookies at the end of the execution of this script*/
setcookie("cookie1","cookie 1 data", time() - 123123);
setcookie("cookie2","cookie 2 data", time() + 54326);
setcookie("cookie3","cookie 3 data", time());
?>
/****end file1.php**/
/*****file2.php****/
<?php
$url = "http://[host][uri]/file1.php";
/*insert desired host and
uri*/
$browser_id = "some crazy browser";
$curl_handle = curl_init();
$options = array
(
CURLOPT_URL=>$url,
CURLOPT_HEADER=>true,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_FOLLOWLOCATION=>true,
CURLOPT_USERAGENT=>$browser_id
);
curl_setopt_array($curl_handle,$options);
$server_output = curl_exec($curl_handle);
curl_close($curl_handle);
/*construct the http search pattern for cookies*/
$pattern = "/Set-Cookie:";
$pattern .= "(?P<name>.*?)=(?P<value>.*?); ";
$pattern .= "expires=(?P<expiry_dayname>\w+), ";
$pattern .= "(?P<expiry_day>\d+)-
(?P<expiry_month>\w+)-(?P<expiry_year>\d+) ";
$pattern .= "(?P<expiry_hour>\d+):
(?P<expiry_minute>\d+):(?P<expiry_second>\d+) ";
$pattern .= "(?P<expiry_zone>\w+)/";
preg_match_all($pattern,$server_output,$matches);
$table_string = "
<h1>cookie information table</h1>
<table border='1'>
<tr>
<td>cookie name</td>
<td>value</td>
<td>expiry day</td>
<td>expiry date</td>
<td>expiry time</td>
<td>expiry timezone</td>
</tr>
";
$i=0;
foreach($matches[name] as $cookie_name)
{
$table_string .= "
<tr>
<td>$cookie_name</td>
<td>{$matches[value][$i]}</td>
<td>{$matches[expiry_dayname][$i]}</td>
<td>{$matches[expiry_day][$i]}-
{$matches[expiry_month][$i]}-
{$matches[expiry_year][$i]}</td>
<td>{$matches[expiry_hour][$i]}:
{$matches[expiry_minute][$i]}:
{$matches[expiry_second][$i]}</td>
<td>{$matches[expiry_zone][$i]}</td>
</tr>
";
$i++;
}
$table_string .= "</table>";
echo $table_string;
?>
/****end file2.php**/
i based this code on the following http header:
(obtained by going: echo $server_output; in file2.php)
HTTP/1.1 200 OK Date: Thu, 30 Jul 2009 07:10:07 GMT
Server: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11
OpenSSL/0.9.8i PHP/5.2.9 X-Powered-By: PHP/5.2.9
Set-Cookie: cookie1=cookie+1+data; expires=Wed,
29-Jul-2009 03:23:27 GMT Set-Cookie
cookie2=cookie+2+data; expires=Wed, 29-Jul-2009 03:23:27
GMT Set-Cookie: cookie3=cookie+3+data; expires=Wed,
29-Jul-2009 03:23:27 GMT Content-Length: 196
Content-Type: text/html
if your header differs from this one then $pattern in file2.php
will need to be modified accordingly. hopefully this code will save
you a lot of time though!
----
Server IP: 117.55.229.1
Probable Submitter: 150.101.100.168
----
Manual Page -- http://www.php.net/manual/en/function.curl-exec.php
Edit -- https://master.php.net/note/edit/92580
Del: integrated -- https://master.php.net/note/delete/92580/integrated
Del: useless -- https://master.php.net/note/delete/92580/useless
Del: bad code -- https://master.php.net/note/delete/92580/bad+code
Del: spam -- https://master.php.net/note/delete/92580/spam
Del: non-english -- https://master.php.net/note/delete/92580/non-english
Del: in docs -- https://master.php.net/note/delete/92580/in+docs
Del: other reasons-- https://master.php.net/note/delete/92580
Reject -- https://master.php.net/note/reject/92580
Search -- https://master.php.net/manage/user-notes.php