Thursday, October 22, 2009

file_get_contentsのCookie取得のサンプル

$url = "http://hoge.com/";
$data = array();

$data['TITLE'] = "hhh";
$data['NAME'] = "aaa";

$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data),

));
$content = file_get_contents($url, false, stream_context_create($options));


$nLines = count( $http_response_header ); // after file_get_contents
$match = "/Set-Cookie: JSESSIONID=(.+):/";
$cookie = "";
$matches = array();
for ( $i = 0; $i < $nLines; $i++ )
{
$line = $http_response_header[$i];
if (preg_match( $match,$line,$matches))
{
$cookie = $matches[1];
break;
}
}
echo $cookie;

No comments: