How can I make a proxy call to this Wordpress PHP function, one time without a parameter, other time with?

121 views Asked by At

So in Wordpress, I have a hook for an ajax action, that looks like this:

add_action( 'wp_ajax_myhook', __NAMESPACE__ .'\myhook' );
function myhook() {

    $username = "[email protected]";
    $password = "password!";
    $url = "http://00.00.000.1:8080/api/v1/generalsite";

    
    $options = array('http' =>
        array(
            'method'=>"GET",            
            'header' => 
                        "Authorization: Basic " . base64_encode("$username:$password") . "\r\n" .
                        "Content-type: application/json\r\n" .
                        "Accept: application/json"
                    
        )
    );    
    $context = stream_context_create($options);    
    $file = file_get_contents($url, false, $context);    
    $dataToShow = (array) json_decode($file);    
    wp_send_json($dataToShow);
}

From a separate Javascript file that runs on the browser side, I have a jquery ajax call made to admin-ajax, which calls this function. This function looks like:

async function getmydata(){

    
    var data = {
        'action': 'myhook',
        'datatosend': JSON.stringify(someJSON)
    }

    const datapulleddown = $.ajax({
           url: wp.ajax.settings.url,
           type: "POST",
           data: data ,
           dataType: 'json',
           success: function (response, status) {
                console.log(`status is ${status}`);
                console.log(`data is ${data}`);
                thedataIwant = response;
           },
           error: function(jqXHR, textStatus, errorThrown) {
              console.log(`jqXHR is ${jqXHR}, textStatus is ${textStatus}, errorThrown is ${errorThrown}`);
              console.log(data);
           }
       });
    return datapulleddown;
}

That grabs a json from the URL w/ the authentication. However, I want to be able to call that hook again in a second browser side ajax call, but with a parameter so as to grab data from http(colon)//00(dot)00(dot)000(dot)1(colon)8080/api/v1/generalsite/NEWPARAM. I'm rather inexperienced w/ PHP but I know in PHP I can pass a standard function a default parameter. How would I write a new JS ajax call to give the url NEWPARAM, and alter the PHP function so I could pass it to this new ajax call with NEWPARAM is a parameter to myhook?

0

There are 0 answers