当前位置:Gxlcms > PHP教程 > php发送post的问题

php发送post的问题

时间:2021-07-01 10:21:17 帮助过:18人阅读

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

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);

回复内容:

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

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);

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

人气教程排行