Heim  >  Artikel  >  Backend-Entwicklung  >  php发送post的问题

php发送post的问题

WBOY
WBOYOriginal
2016-06-06 20:35:03931Durchsuche

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'

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn