Send HTTP Post with Array

I'm trying to use this nice function:

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

to send a POST command to a specific url, my problem is i'm trying to send the post paramters in a form of an array, something like this

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

however i'm not able to do that, calling the function with :

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

always send the data to the post in the form:

username: myusername
password: mypassword

How can I avoid this (without using curl)? Thanks a lot.

Thanks

Yehia


也许有那个?

<?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/41084.html

上一篇: 向Node js中的休息服务发送https请求的步骤

下一篇: 用数组发送HTTP Post