Home  >  Article  >  Backend Development  >  php发送post的问题

php发送post的问题

WBOY
WBOYOriginal
2016-06-06 20:35:03929browse

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'

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn