2
votes

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

3
And what happens when you try sending the data that way? What does a var_dump[$_POST] reveal on the receiving side?Pekka
Zend framework is handling the POST on this specific url and is expecting the data in the form login[username], login[password], and i am trying to simulate thatYehia A.Salam
ah. If ZF works with this method, then it must be possible to do somehow. Deleted my answer saying otherwisePekka
i can get username: myusername, password: mypassword, as if the array in the $data is somwhow flattenedYehia A.Salam
I see. I think you'd have to transfer a real associative array array("username" => $email, "password" => "") but how to do that in curl, I don't knowPekka

3 Answers

3
votes

Maybe with that ?

<?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 . "\r\n";
       }

       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 );
?>
0
votes
 $login_post = array(
'login' => array(
'username' => $email,
'password' => ''))
0
votes

Try using stream_context_create

$url = 'http://your_url.com/path';
$data = array('login' => 'usrnname', 'password' => '**');

$opt = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($opt);
$result = file_get_contents($url, false, $context);

var_dump($result);

This method doesnt use curl.