2

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

5
  • And what happens when you try sending the data that way? What does a var_dump[$_POST] reveal on the receiving side? Commented Oct 7, 2010 at 11:52
  • 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 that Commented Oct 7, 2010 at 11:54
  • ah. If ZF works with this method, then it must be possible to do somehow. Deleted my answer saying otherwise Commented Oct 7, 2010 at 11:56
  • i can get username: myusername, password: mypassword, as if the array in the $data is somwhow flattened Commented Oct 7, 2010 at 11:56
  • 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 know Commented Oct 7, 2010 at 11:57

3 Answers 3

3

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 );
?>
Sign up to request clarification or add additional context in comments.

Comments

0
 $login_post = array(
'login' => array(
'username' => $email,
'password' => ''))

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.