I have a WordPress website with a newsletter subscription code written in JavaScript and PHP. Everything works fine on localhost, but when I upload the website to hosting, the page freezes after clicking the button, and after a while an error message appears in the console: curl: (28) Failed to connect to (my api url) port 443 after 128807 ms: Connection timed out
Javascript code
document.getElementById('submit_newsletter').addEventListener('click', function (e) {
e.preventDefault();
var email = document.getElementById('KPGH_newsletter_email').value;
var nonce = document.getElementById('KPGH_message_nonce').value;
var validationError = false;
if (!validateEmail(email)) {
document.getElementById('KPGH_email_err').style.display = 'block';
validationError = true;
} else {
document.getElementById('KPGH_email_err').style.display = 'none';
}
if (validationError) {
return;
}
var message = new FormData();
message.append('action', 'KPGH_newsletter_form');
message.append('KPGH_newsletter_email', email);
message.append('KPGH_message_nonce', nonce);
console.log("Sending AJAX request to: " + ajax_url);
console.log("FormData: ", message);
var xhr = new XMLHttpRequest();
xhr.open('POST', ajax_url, true);
xhr.responseType = 'json';
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 400) {
console.log("AJAX success response: ", xhr.response);
if (xhr.response.success) {
document.getElementById('KPGH_newsletter_succes').innerHTML = xhr.response.data;
document.getElementById('KPGH_newsletter_succes').style.display = 'block';
setTimeout(function () {
document.getElementById('KPGH_newsletter_succes').style.display = 'none';
}, 4500);
} else {
document.getElementById('KPGH_newsletter_error').innerHTML = xhr.response.data;
document.getElementById('KPGH_newsletter_error').style.display = 'block';
setTimeout(function () {
document.getElementById('KPGH_newsletter_error').style.display = 'none';
}, 4500);
}
} else {
console.log("AJAX error response: ", xhr.response);
document.getElementById('KPGH_newsletter_error').innerHTML = xhr.statusText;
document.getElementById('KPGH_newsletter_error').style.display = 'block';
setTimeout(function () {
document.getElementById('KPGH_newsletter_error').style.display = 'none';
}, 4500);
}
};
xhr.onerror = function () {
console.log("AJAX request failed");
document.getElementById('KPGH_newsletter_error').innerHTML = "Request failed";
document.getElementById('KPGH_newsletter_error').style.display = 'block';
setTimeout(function () {
document.getElementById('KPGH_newsletter_error').style.display = 'none';
}, 4500);
};
xhr.send(message);
});
function validateEmail(email) {
var re = /^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
PHP Code (functions.php)
if (!function_exists('KPGH_newsletter_form')) {
function KPGH_newsletter_form() {
// Sprawdź czy nonce jest ustawione
if (!isset($_POST['KPGH_message_nonce']) || !wp_verify_nonce($_POST['KPGH_message_nonce'], 'KPGH-ajax-message-submit')) {
error_log('Nonce verification failed');
wp_send_json_error('Nonce verification failed');
}
$url = 'https://xxxxxxx/api/2/auth/xxxxxxxxxx';
$login = 'xxxxxxxx';
$password = 'xxxxxx';
$headers = array(
'ACCEPT: application/x-www-form-urlencoded',
'USER_AGENT: xxxxx'
);
// Wysyłanie żądania uwierzytelniającego
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_USERPWD => $login . ':' . $password,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
));
$tokenResponse = curl_exec($curl);
if ($tokenResponse === false) {
error_log('Curl error: ' . curl_error($curl));
curl_close($curl);
wp_send_json_error('Curl error: ' . curl_error($curl));
}
$tokenData = json_decode($tokenResponse);
if (json_last_error() !== JSON_ERROR_NONE || !isset($tokenData->data)) {
error_log('Token not received or json decode error: ' . json_last_error_msg());
curl_close($curl);
wp_send_json_error('Authentication failed');
}
$token = $tokenData->data;
error_log('Token received: ' . $token);
curl_close($curl);
// Przygotowanie danych kontaktowych
$contact = array(
'contact' => array(
array(
'email' => sanitize_email($_POST['KPGH_newsletter_email']),
'category' => array('26' => '1'),
'agreement' => array('1' => '1'),
'contactAccess' => array('ID_CONTACT_ACCESS' => 1),
'source' => array(
'utm_campaign' => 'CAMPAIGN_NAME',
'utm_content' => 'CAMPAIGN_CONTENT',
'utm_medium' => 'CAMPAIGN_MEDIUM',
'utm_source' => 'CAMPAIGN_SOURCE',
'utm_term' => 'CAMPAIGN_TERM',
)
)
)
);
// Wysyłanie danych kontaktowych
$url = 'xxxxxxxx/api/2/contact/';
$headers[] = 'IPRESSO_TOKEN: ' . $token;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($contact),
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
));
$contactResponse = curl_exec($curl);
if ($contactResponse === false) {
error_log('Curl error: ' . curl_error($curl));
curl_close($curl);
wp_send_json_error('Curl error: ' . curl_error($curl));
}
$contactData = json_decode($contactResponse);
if (json_last_error() !== JSON_ERROR_NONE || !isset($contactData->data->contact)) {
error_log('Contact not received or json decode error: ' . json_last_error_msg());
curl_close($curl);
wp_send_json_error('Adding contact failed');
}
$code = null;
foreach ($contactData->data->contact as $child) {
$code = $child->code;
}
error_log('Response code: ' . $code);
curl_close($curl);
switch ($code) {
case 303:
case 302:
wp_send_json_success('Podany adres już jest w naszej bazie danych.');
break;
case 200:
case 201:
wp_send_json_success('Twoja email został dodany do naszej bazy. Dziękujemy!');
break;
default:
wp_send_json_error('Przepraszamy, nie udało się dodać twojego adresu email do naszej bazy');
}
}
}
add_action('wp_ajax_KPGH_newsletter_form', 'KPGH_newsletter_form');
add_action('wp_ajax_nopriv_KPGH_newsletter_form', 'KPGH_newsletter_form');
On localhost it works great, here is the video of the newsletter fully working:
https://streamable.com/7wkvt2
So could it be a hosting issue?
error
I tried disabling other plugins, but unfortunately it doesn’t help.
wiktor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.