首页 >后端开发 >php教程 >php发送post的问题

php发送post的问题

WBOY
WBOY原创
2016-06-06 20:35:03962浏览

post.php向getPost.php发送post
getPost.php里面只有这句 var_dump($_POST);
为什么getPost.php里面是空 反而post.php里面得到了值。
谢谢了

<code>function do_post_request($url, $data, $optional_headers = null)
{
    $params = array('http' => array(
        'method' => 'POST',
        'content' => $data
    ));
    if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
    }
    $ctx = stream_context_create($params);
    $fp = @fopen($url, 'rb', false, $ctx);
    if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
    }
    $response = @stream_get_contents($fp);
    if ($response === false) {
        throw new Exception("Problem reading data from $url, $php_errormsg");
    }
    return $response;
}

$postdata = array('name' => 'lichen');
$postdata = http_build_query($postdata);
echo do_post_request('http://localhost/getPost.php', $postdata);
</code>

回复内容:

post.php向getPost.php发送post
getPost.php里面只有这句 var_dump($_POST);
为什么getPost.php里面是空 反而post.php里面得到了值。
谢谢了

<code>function do_post_request($url, $data, $optional_headers = null)
{
    $params = array('http' => array(
        'method' => 'POST',
        'content' => $data
    ));
    if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
    }
    $ctx = stream_context_create($params);
    $fp = @fopen($url, 'rb', false, $ctx);
    if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
    }
    $response = @stream_get_contents($fp);
    if ($response === false) {
        throw new Exception("Problem reading data from $url, $php_errormsg");
    }
    return $response;
}

$postdata = array('name' => 'lichen');
$postdata = http_build_query($postdata);
echo do_post_request('http://localhost/getPost.php', $postdata);
</code>

$param请加上'header' => 'Content-type:application/x-www-form-urlencoded'

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn