Skip to content

Instantly share code, notes, and snippets.

@michaeluno
Last active December 13, 2015 19:58
Show Gist options
  • Save michaeluno/4966435 to your computer and use it in GitHub Desktop.
Save michaeluno/4966435 to your computer and use it in GitHub Desktop.
An example to submit POST data with arrays to a web page that has a POST form.
<?php
if ( isset( $_POST['login'] ) )
die( '<pre>' . print_r( $_POST, true ) . '</pre>' ) ;
?>
<html>
<body>
<form accept-charset="UTF-8" action="" method="post">
<ul class="clear-float">
<li><label for="user_login">Login ID</label>
<input id="user_login" name="user[login]" size="13" type="text" value="" />
</li>
<li><label for="user_password">Password</label>
<input id="user_password" name="user[password]" size="13" type="password" value="" />
</li>
</ul>
<label for="cookie_remember_me" style="margin: 5px 0 12px 0">
<input name="cookie[remember_me]" type="hidden" value="off" />
<input checked="checked" id="cookie_remember_me" name="cookie[remember_me]" type="checkbox" value="on" />
&nbsp;Remember me
</label>
<input id="from_desk" name="from_desk" type="hidden" value="1" />
<input class="button primary" name="login" type="submit" value="Login" />
</form>
</body>
</html>
$url = 'http://localhost/phptests/Forms/post.php';
$response = wp_remote_post( $url, array(
'method' => 'POST',
'body' => array(
'user' => array(
'login' => 'myname',
'pasword' => 'mypassword',
),
'login' => 'Login',
'from_desk' => false,
'cookie' => array(
'remember_me' => 'on'
),
),
)
);
echo '<h3>Response</h3>';
if ( is_wp_error( $response ) )
die( 'wp error' );
preg_match( '/charset=(.+?)($|\s)/i', $response['headers']['content-type'], $matches );
$charset = isset( $matches[1] ) ? $matches[1] : '';
echo '<pre>';
$result = @mb_convert_encoding ( $response['body'] , get_bloginfo( 'charset' ), $charset );
echo @htmlspecialchars( $result , ENT_QUOTES, get_bloginfo( 'charset' ) );
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment