What is the correct way to call a PHP Function with multiple parameters from the AJAX Handler?

Q: What is the correct way to call a PHP Function with multiple parameters from the AJAX Handler?

Q: Am I calling the php function correctly?


Page Element
The user clicks a button.

Action:
The click should connect to an external server using the following params:

  1. User Name 2. IP Address 3. Port 4. Public Key 5. Service

Purpose:
Send an vote string (from host A) to an external Gaming server (host B).
I do not need to update any wordpress content.

WordPress Version: 5.4.1
I put my custom php code into “/wp-contents/plugins/my-plugin/votifier.php”

I made sure the custom plugin is activated.

I have WordPress in Debug mode. i.e. Debug mode is true.

The Button

<div id="frm_field_61_container">
<button type="button">Try it</button>
</div>

WordPress JQuery with AJAX

jQuery(document).ready( function($) {
 $("#frm_field_61_container").click(function(){

      jQuery.ajax({

         type : "post",

         dataType : "json",

         url : myAjax.ajaxurl,

         data : {    action: "my_vote_count"
                    ,username: $('input[name="item_meta[59]"]').val()
                    ,key: $.trim($("#field_yjr62").val())
                    ,ip: $('input[name="item_meta[40]"]').val()
                    ,port: $('input[name="item_meta[42]"]').val()
                    ,service: "Votifier"
                },

        success:    function(data,status,xhr){
                    alert("Data: " + data);
                    alert("Status: " + status);
                    alert("xhr: " + xhr);
                }
      });   
   });
});

My Custom PHP Script

<?php


function mainVotifier($username, $public_key, $server_ip, $port, $service_name) {

define( 'VOTE_FORMAT', 'VOTEn%sn%sn%sn%dn' );
define( 'PUBLIC_KEY_FORMAT', '-----BEGIN PUBLIC KEY-----n%sn-----END PUBLIC KEY-----' );

$key    = wordwrap($public_key, 65, "n", true);
$key    = sprintf(PUBLIC_KEY_FORMAT, $key);


  if (php_sapi_name() !== 'cli') {
            //Detect proxy and use correct IP.
            $address = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
        } else {
            //Script is run via CLI, use server name.
            $address = $_SERVER['SERVER_NAME'];
        }

    $data = sprintf(VOTE_FORMAT, $service_name, $username, $address, time());
    openssl_public_encrypt($data, $crypted, $key);
    $socket = @fsockopen($server_ip, $port);

    if ($socket) {
        if (fwrite($socket, $crypted)) {
            fclose($socket);
            return true;
        }
    }

}

?>

/* ===== WP_ENQUEUE SCRIPTS ==== */

/* ===== JQuery Ajax Script to call votifier.php ==== */
add_action( 'wp_enqueue_scripts', 'my_script_enqueuer' );
function my_script_enqueuer() {

   wp_enqueue_script( 'my_voter_script',  plugins_url('sendvote.js', __FILE__), array( 'jquery'),   '1.0', true);

   wp_localize_script( 'my_voter_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));

}

/* ===== My AJAX Handler ==== */

/* add_action( 'wp_ajax_nopriv_my_vote_count', 'my_ajax_handler' ); */ 
add_action( 'wp_ajax_my_vote_count', 'my_ajax_handler');
function my_ajax_handler() {

/*  check_ajax_referer('votifier_response_key'); */

    $username       = $_POST["username"];
    $public_key     = $_POST['key'];
    $server_ip      = $_POST["ip"];
    $port           = $_POST["port"];   
    $service_name   = $_POST["service"];

   echo "mainVotifier("" . $username . "", "" . $public_key . "", "" . $server_ip . "", "" . $port . "", "" . $service_name . "");";


    wp_die(); // All ajax handlers die when finished

}

Console Results

The console results are good, but the other server is not responding.
I tested the PHP code locally from my personal computer and Server B is responding.


Request URL:https://bestminecraftserverlist.com/wp-admin/admin-ajax.php
Request Method:POST
Remote Address:192.169.170.42:443
Status Code:
200
Version:HTTP/2

Go to Source
Author: ScottUSA