您的位置:首页 > 其它

file_get_contents 模拟POST数据发送

2017-09-20 15:41 597 查看
<?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);//
发送(关键是第三个参数)

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: