'Ascora Enquiry API Returning "No Content" when utilising wp_remote_post
I am trying to submit enquiries into Ascora using their Enquiry API.
I have created a WordPress plugin and I am trying to send the payload using wp_remote_post.
If any of you kind folk can spot my mistake, and let me know how I can get this working, I would greatly appreciate it.
Here is the code that I have written:
if( ! class_exists( 'Forminator_Ascora_Integration' ) ) {
class Forminator_Ascora_Integration {
private $endpoint, $token;
public function __construct() {
$this->endpoint = "https://api.ascora.com.au/Enquiry";
$this->token = "OMITTED-FOR-SO";
add_action( 'forminator_custom_form_submit_before_set_fields', array( $this, 'after_forminator_submission'), 10, 3 );
}
public function after_forminator_submission( $entry, $form_id, $submission_data ) {
$submitted_enquiry = null;
// General Enquiries
if( $form_id == 1564 ) {
$submitted_enquiry = array(
'firstName' => $submission_data[0]['value'],
'lastName' => $submission_data[1]['value'],
'companyName' => $submission_data[2]['value'],
'email' => $submission_data[3]['value'],
'phone' => $submission_data[4]['value'],
'enquiryDescription' => $submission_data[5]['value'],
);
}
if( is_array ($submitted_enquiry ) ) {
$this->send_data_to_ascora( $submitted_enquiry );
}
}
private function send_data_to_ascora( $submitted_enquiry = array() ) {
$default_enquiry = array(
'companyName' => '',
'firstName' => '',
'lastName' => '',
'email' => '',
'phone' => '',
'mobile' => '',
'addressLine1' => '',
'addressLine2' => '',
'addressSuburb' => '',
'addressPostcode' => '',
'addressCountry' => '',
'enquiryDescription' => '',
);
$enquiry = array_merge( $default_enquiry, $submitted_enquiry );
$payload = array(
'method' => 'POST',
'headers' => array(
'Auth' => $this->token,
'Content-Type' => 'application/json; charset=utf-8'
),
'body' => json_encode( $enquiry ),
'timeout' => 30,
'httpversion' => '1.1',
);
$response = wp_remote_post( $this->endpoint, $payload );
wp_mail( 'OMITTED-FOR-SO', 'Payload Sent', "<pre>" . var_export( $payload, true ) . "</pre>", array('Content-Type: text/html; charset=UTF-8') );
wp_mail( 'OMITTED-FOR-SO', 'API Response', "<pre>" . var_export( $response, true ) . "</pre>", array('Content-Type: text/html; charset=UTF-8') );
$resonse_error = null;
if( is_wp_error( $response ) ) {
$response_error = 'Error Found:' . $response->get_error_message();
}
if( $response_error ) {
error_log( print_r( $response_error, true ) );
}
}
}
}
new Forminator_Ascora_Integration();
As you can see in the source code, for testing purposes I am sending myself two emails.
Here is the content of those emails respectively.
Payload:
array (
'method' => 'POST',
'headers' =>
array (
'Auth' => 'OMITTED-FOR-SO',
'Content-Type' => 'application/json; charset=utf-8',
),
'body' => '{"companyName":"OMITTED-FOR-SO","firstName":"OMITTED-FOR-SO","lastName":"OMITTED-FOR-SO","email":"OMITTED-FOR-SO","phone":"OMITTED-FOR-SO","mobile":"","addressLine1":"","addressLine2":"","addressSuburb":"","addressPostcode":"","addressCountry":"","enquiryDescription":"Testing."}',
'timeout' => 30,
'httpversion' => '1.1',
)
API Response:
array (
'headers' =>
Requests_Utility_CaseInsensitiveDictionary::__set_state(array(
'data' =>
array (
'server' => 'Microsoft-IIS/10.0',
'x-powered-by' => 'ASP.NET',
'access-control-allow-origin' => '*',
'access-control-allow-headers' => 'Content-Type, Authorization, Auth',
'access-control-allow-methods' => 'GET, POST, PUT, DELETE, OPTIONS',
'date' => 'Fri, 20 May 2022 05:04:08 GMT',
),
)),
'body' => '',
'response' =>
array (
'code' => 204,
'message' => 'No Content',
),
'cookies' =>
array (
),
'filename' => NULL,
'http_response' =>
WP_HTTP_Requests_Response::__set_state(array(
'response' =>
Requests_Response::__set_state(array(
'body' => '',
'raw' => 'HTTP/1.1 204 No Content
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type, Authorization, Auth
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Date: Fri, 20 May 2022 05:04:08 GMT
Connection: close
',
'headers' =>
Requests_Response_Headers::__set_state(array(
'data' =>
array (
'server' =>
array (
0 => 'Microsoft-IIS/10.0',
),
'x-powered-by' =>
array (
0 => 'ASP.NET',
),
'access-control-allow-origin' =>
array (
0 => '*',
),
'access-control-allow-headers' =>
array (
0 => 'Content-Type, Authorization, Auth',
),
'access-control-allow-methods' =>
array (
0 => 'GET, POST, PUT, DELETE, OPTIONS',
),
'date' =>
array (
0 => 'Fri, 20 May 2022 05:04:08 GMT',
),
),
)),
'status_code' => 204,
'protocol_version' => 1.1,
'success' => true,
'redirects' => 0,
'url' => 'https://api.ascora.com.au/Enquiry',
'history' =>
array (
),
'cookies' =>
Requests_Cookie_Jar::__set_state(array(
'cookies' =>
array (
),
)),
)),
'filename' => NULL,
'data' => NULL,
'headers' => NULL,
'status' => NULL,
)),
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
