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

How to send the body in wp_remote_post as “raw”?

I got this one working, you have to pass the json_encoded array on the body parameter of the request. $customer_details = [ ‘first_name’ => $first_name, ‘last_name’ => $last_name, ’email’ => $email, ]; $customer_details[‘body’] = json_encode($customer_details); $customer = $api->createUpdateCustomer($customer_details); public function createUpdateCustomer($customer_details) { $response = $this->apiCall( “2.0/customers”, ‘POST’, $customer_details); $result = json_decode($response); return $result; } public … Read more

How to code auto-retry for API call

First you should save the data somewhere in your wordpress. I personally like to use custom tables. Then use a schedule (cron job) to check if there is new data. Then try to submit the data. If you sumitted the data successfully delete it or mark your local data as successfully submitted. Else retry it … Read more