file_get_contents post $cookie, how?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wildmachine
Forum Newbie
Posts: 1
Joined: Fri Jul 16, 2010 11:49 am

file_get_contents post $cookie, how?

Post by wildmachine »

Below code is works,

Code: Select all

<?
$data = array   
(   
    'msg_author' => 'poster',
    'msg_body' => 'gsd958gjsj sdj958fdsj',
    'event' => 'comment',
    'pid' => '15',
    'submit' => 'OK',
);

function Post($url, $post = null)   
{   
    $context = array();   
    if (is_array($post))   
    {   
        ksort($post);   
        $context['http'] = array   
        (   
            'method' => 'POST',
            'header' => 'Cookie: 62be229b68f9a8202715cdd8c7092e31=63b33644fd6ed03214ed3887d7ead743; expires=Wed, 28-Jul-2010 13:53:37 GMT; path=/; cpg140_data=YToyOntzOjI6IklEIjtzOjMyOiI5NzMxMzFkOTJhZDU3MzExZTZmODMyZDlhMDRkNjZjNyI7czoyOiJhbSI7aToxO30%3D; expires=Fri, 13-Aug-2010 13:53:37 GMT; path=/;\r\n',
            'content' => http_build_query($post, '', '&'),   
        );   
    }   
    return file_get_contents($url, false, stream_context_create($context));   
}   
   
echo Post('http://domain/post.php', $data);
?>
But if put cookie value into $cookie, the code will never works, WHY???

Code: Select all

<?
$data = array   
(   
    'msg_author' => 'poster',
    'msg_body' => 'gsd958gjsj sdj958fdsj',
    'event' => 'comment',
    'pid' => '15',
    'submit' => 'OK',
);

$cookie="Cookie: 62be229b68f9a8202715cdd8c7092e31=63b33644fd6ed03214ed3887d7ead743; expires=Wed, 28-Jul-2010 13:53:37 GMT; path=/; cpg140_data=YToyOntzOjI6IklEIjtzOjMyOiI5NzMxMzFkOTJhZDU3MzExZTZmODMyZDlhMDRkNjZjNyI7czoyOiJhbSI7aToxO30%3D; expires=Fri, 13-Aug-2010 13:53:37 GMT; path=/;\r\n";

function Post($url, $post = null)   
{   
    $context = array();   
    if (is_array($post))   
    {   
        ksort($post);   
        $context['http'] = array   
        (   
            'method' => 'POST',
            'header' => $cookie,
            'content' => http_build_query($post, '', '&'),   
        );   
    }   
    return file_get_contents($url, false, stream_context_create($context));   
}   
   
echo Post('http://domain/post.php', $data);
?>
Post Reply