file_get_contents 模拟POST数据发送

来源:互联网 发布:阿里妈妈采集软件 编辑:程序博客网 时间:2024/06/08 14:27
<?php
        $postDate = array(
            'title' => '天气好',
            'content' => '今天是星期三',
        ); // 要发送的数据

        $postDate = http_build_query($postDate); // 使用给出的关联(或下标)数组生成一个 url-encoded 请求字符串。大笑

        $opts = array(
            'http'=>array(
                'method'=>"POST",// post请求
                'header'=>"Host:localhost\r\n" .
                    "Content-type:application/x-www-form-urlencoded\r\n" .
                    "Content-length:".strlen($postDate)."\r\n", // 首部 \r\n 回车换行
                'content' => $postDate,// 内容
            )
        );

        $context = stream_context_create($opts);//创建并返回一个资源流上下文,该资源流中包含了提前设定的所有参数的值。
        file_get_contents("http://localhost/index2.php",false,$context);// 发送(关键是第三个参数)
?>
阅读全文
0 0