Send HTTP Post with Array - PHP -
i'm trying use 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 $url, $php_errormsg"); } $response = @stream_get_contents($fp); if ($response === false) { throw new exception("problem reading data $url, $php_errormsg"); } return $response; }
to send post command specific url, problem i'm trying send post paramters in form of array, this
login[username]: myusername login[password]: mypassword,
however i'm not able that, calling function :
$login_post = array('login[username]' => $email, 'login[password]' => ''); do_post_request(gethost($website['code']), $login_post);
always send data post in form:
username: myusername password: mypassword
how can avoid (without using curl)? lot.
thanks
yehia
maybe ?
<?php // define post data $donnees = array( 'login' => 'test', 'password' => '******' ); function http_build_headers( $headers ) { $headers_brut = ''; foreach( $headers $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 ); ?>
Comments
Post a Comment