I am new to WordPress and I have a client requirement to hit their API, which accepts a POST request and I just can’t find out the way to achieve this.
I have to add the chat bot which hits API whenever user presses enter and display response of API.
1 Answer
WordPress has several functions for sending requests via PHP, including wp_remote_post()
, which you can use to send a POST request to an API. Then you can use wp_remote_retrieve_body()
to handle the response. It would look something like:
$response = wp_remote_post( 'http://example.org/api' );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
If you need to interact with the API via JavaScript, then WordPress doesn’t get involved, and you would do it the same way you’d do it any any JavaScript application.