• Resolved ayice

    (@ayice)


    Hi

    I am trying to make a call to admin-ajax.php through the wp hook: wp_remote_request.
    I have read the documentation and I can’t seem to find any problems. I have tried to use the wp_remote_post hook as well but with same outcome.

    I am trying to make the request fire an action in the admin-ajax file and return the data.
    The request is fired after it receives a post request from a remote site I am also managing, where I am sending the nonce etc.

    The request:

    $args = array(
                    'method' => 'POST',
                    'timeout'     => '50',
                    'redirection' => '5',
                    'blocking'    => true,
                    'headers'     => array(),
                    'cookies'     => array(),
                    'body'        => array(
                        'offset' => '0',
                        'limit' => '100',
                        'action' => 'wordfence_loadIssues',
                        'nonce' => $_POST['nonce']
                    )
                );
    
                $response = wp_remote_request(admin_url('admin-ajax.php'), $args);
    
                $response_code = wp_remote_retrieve_response_code($response);
                $response_body = wp_remote_retrieve_body($response);
    
                wp_send_json_success(array('code' => $response_code, 'body' => 
                $response_body, 'nonce' => $_POST['nonce']));
    
                exit();

    Hope you can help me or point me in the right direction.
    Thank you 🙂

Viewing 15 replies - 1 through 15 (of 15 total)
  • You are already in PHP with WP loaded, so why would you want to make a remote request (which is really local) to load WP again?
    How would the nonce match, when it is from a separate site?
    If you want the remote site to talk to your WP site, use the REST API and write your own route.

    Thread Starter ayice

    (@ayice)

    Hi @joyously

    Maybe I misunderstood what wp_remote_request does.
    All I need is to make a request to the admin-ajax.php file to get some data and return this to my other site 🙂

    What I want is to access the data in the 'wordfence_loadIssues' action and return it to my other site 🙂

    When writing my own route, can I return an this data ?
    I’m new in trying to work with WP like this, so I don’t know if this is possible ? 😀
    Hope you can help me

    There is no data in the admin-ajax.php file. It is a way for an authorized client to interact with the server.
    There is no data in an action. An WP action is a place in the code where extra functionality can be executed.

    If you want to trigger a particular functionality, you can call the function directly or call do_action() with the right parameters to get all the registered functionality for that action. But actions don’t return any data, they simply execute code that is added.
    If the data you want to return is public, you don’t really need to worry about security. Otherwise, you need to code it so that only the authorized requests get the data.
    You probably should ask in the WordFence support forum for the best way to get the data you want.

    Thread Starter ayice

    (@ayice)

    Oh okay, that makes sense.

    I know that an action, is not containing data, but the request to call that action returns data, maybe that is more accurate ?

    I will try do_action() and see if that does the trick 🙂

    I already spoke with the WordFence guys, but they didn’t think this was possible, but it is, I already made it work from the frontend, but now I want PHP to return the data 🙂

    Specifically, calling do_action() means that you don’t get any data returned from that call. If you called the plugin’s function directly, you might get the data returned (not sure what the function does).
    Basically, action functions do not return data to their caller. But they can echo or send or whatever; just not to the caller of the function.

    Thread Starter ayice

    (@ayice)

    Just to clarify, when I send an AJAX request to the admin-ajax.php file with an ‘action’ in the body. Am I not telling admin-ajax to run a certain functionality and maybe return a value ?

    Thread Starter ayice

    (@ayice)

    Basically I want a function that kinda does the same as an HTTP request to the admin-ajax.php file. But within php code? Is this possible ? 🙂

    Yes, a request to admin-ajax.php executes the action, and anything echoed is returned (whether it’s JSON or HTML). But it loads another, separate instance of WP for that. And you can do the same with the REST API, but the authorization mechanism is different.

    Thread Starter ayice

    (@ayice)

    Okay great, The problem is, I haven’t made the action, its inside the plugin.

    Can you give me an example of how to trigger this action from php, thank you ?

    Perhaps you can use output buffering.
    Call ob_start() and then do_action() and then one of the ob_ functions to end the bufferning (depending on what you want…)
    https://www.php.net/manual/en/function.ob-start

    Thread Starter ayice

    (@ayice)

    Okay thank you @joyously I will look into it 😀
    I was hoping for a way to request the rest api from a wp_hook or something. But I see that is not possible 🙂

    Thread Starter ayice

    (@ayice)

    @joyously Hi again Joy
    Just to let you know it works now, but not with the do_action() or any other wp hooks. I looked through WordFence code and used some of the functions they had in one of their classes, which returned the data I wanted ! 🙂

    So now I am all good for the school project 😉

    Thank you very much for your patience while answering my questions and thank you for spending some time, trying to help me 🙂

    Happy new year 😉

    Sounds great!
    Just be sure you put a check for existence before the call to the plugin functions, or you will get a fatal error when that plugin is deactivated or not installed at all.

    Thread Starter ayice

    (@ayice)

    Great suggestion, hadn’t even considered that thank you again 🙂

    Thread Starter ayice

    (@ayice)

    For people wanting to see the solution I can send you the function I used here:

    function getIssuesData()
    {
        if (class_exists('wfIssues')) {
    
            $issues = wfIssues::shared()->getIssues(0, WORDFENCE_SCAN_ISSUES_PER_PAGE, 0, WORDFENCE_SCAN_ISSUES_PER_PAGE);
            $issueCounts = array_merge(array('new' => 0, 'ignoreP' => 0, 'ignoreC' => 0), wfIssues::shared()->getIssueCounts());
    
            return array(
                'issues' => $issues,
                'issueCounts' => $issueCounts,
            );
        } else {
            return array(
                'body' => 'Have you installed WordFence? We can not find the "wfIssues" class',
                'status' => 404
            );
        }
    }
Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘wp_remote_request returning ‘400 Bad request’’ is closed to new replies.