wp_remote_post code conversion

Use this code : $url=”https://xxxx.yyyyy.zzzzz”; $body_array = array( ‘object_type’ => ‘Leaderboard’, ‘action’ => ‘list’, ‘pagination’ => array( ‘limit’ => 5, ‘order’ => false ), ‘data’ => array( ‘username’ => ‘[email protected]’ ) ); $response = wp_remote_post( $url, array( ‘method’ => ‘POST’, ‘timeout’ => 45, ‘redirection’ => 5, ‘httpversion’ => ‘1.0’, ‘blocking’ => true, ‘headers’ => array( … Read more

Inconsistencies between wp_remote_post and cURL

As discovered above, headers need to be (seems so obvious now) key-value pairs, as opposed to an array of json-style key-values: $headers = array(); $headers[‘Content-Type’] = ‘application/json’; $headers[‘Api-Key’] = ‘{myapikey}’; $headers[‘Siteid’] = ‘99999’; And the body needs to be json so either: ‘body’ => “{\n \”Username\”: \”Siteowner\”,\n \”Password\”: \”apitest1234\”\n}” or ‘body’ => json_encode(array( ‘Username’ => … Read more

wp_remote_post not working with admin-post.php

Thanks to the comment by Sally CJ, it turned out that the issue was that the user was not authenticated when invoking admin-post.php via wp_remote_post. The simplest solution was to ensure that the wp_remote_post had all the current cookies, as follows: $nonce = wp_create_nonce( ‘my-action’ ); foreach ( $_COOKIE as $name => $value ) { … Read more

How to send a HTTP Post request using PHP Curl and WordPress

I managed to solve this by adding the following into my functions.php add_shortcode(‘my_shortode’, ‘my_function’); function my_function () { $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_PORT => “2222”, CURLOPT_URL => “http://11.111.11.111:2222/folder/query”, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => “”, CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => “POST”, CURLOPT_POSTFIELDS => “<root>\r\n <something1>username</something1>\r\n <something2>123456789</something2>\r\n <something3>Hello</something3>\r\n</root>\r\n”, CURLOPT_HTTPHEADER => … Read more