用数组发送HTTP Post

我试图使用这个很好的功能:

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

发送一个POST命令到一个特定的url,我的问题是我试图发送一个数组形式的帖子参数,像这样

login[username]: myusername
login[password]: mypassword,

但我无法做到这一点,调用函数:

            $login_post = array('login[username]' => $email,
                            'login[password]' => '');
        do_post_request(getHost($website['code']), $login_post);

始终以以下格式将数据发送至帖子:

username: myusername
password: mypassword

我怎样才能避免这种情况(不使用卷曲)? 非常感谢。

谢谢

叶海亚


也许有那个?

<?php
// Define POST data

$donnees = array(
'login' => 'test',
'password' => '******' );

function http_build_headers( $headers ) {

       $headers_brut = '';

       foreach( $headers as $name => $value ) {
               $headers_brut .= $name . ': ' . $value . "rn";
       }

       return $headers_brut;
}

$content = http_build_query( $donnees );

// define headers

$headers = http_build_headers( array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Content-Length' => strlen( $content) ) );

// Define context

$options = array( 'http' => array( 'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1) Gecko/20061010 Firefox/2.0',
'method' => 'POST',
'content' => $content,
'header' => $headers ) );

// Create context
$contexte = stream_context_create( $options );

// Send request
$return = file_get_contents( 'http://www.exemple.com', false, $contexte );
?>

 $login_post = array(
'login' => array(
'username' => $email,
'password' => ''))
链接地址: http://www.djcxy.com/p/41083.html

上一篇: Send HTTP Post with Array

下一篇: How to install Composer using Composer?