Call external API in wordpress based on user input

I want to create a simple wordpress website in which I want to display a search box on a page. On searching a string in text/search box, an external API call should be made based on that string and the data returned by the API should be shown to user.
I have successfully referred code from below URL so far – https://rapidapi.com/blog/integrate-external-api-wordpress/.

But I am not able to make a call to API based on string passed in text box.
I have done below code so for in the child theme file page:-

<div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
    <main id="main" <?php generate_do_element_classes( 'main' ); ?>>
        <form action="" method="post">
        Enter query:
        <input type=text name="t1">
        <br>
        <br>
        <input type=submit name="s">
        <?php
        /**
         * generate_before_main_content hook.
         *
         * @since 0.1
         */
        do_action( 'generate_before_main_content' );

        if(isset($_POST['s'])){
        echo "good";    
        $curl = curl_init();

        curl_setopt_array($curl, [
            CURLOPT_URL => "******API URL*****/".$_POST['t1'],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => [
                "x-rapidapi-host: XYZ",
                "x-rapidapi-key: ABc"
            ],
        ]);

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            echo $response;
        }
    }
                

        /**
         * generate_after_main_content hook.
         *
         * @since 0.1
         */
        do_action( 'generate_after_main_content' );
        ?>
        </form>
    </main>
</div>

<?php
/**
 * generate_after_primary_content_area hook.
 *
 * @since 2.0
 */
do_action( 'generate_after_primary_content_area' );

generate_construct_sidebars();

get_footer();

When I click on Submit button, it gives ‘Page not Found’.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

s is a reserved keyword, you cannot reuse the URL parameters and query variables used by WordPress itself. This issue is unrelated to the API request code.

Because your form has an input with the name s, the request is interpreted as a search request. As no posts were found in the WordPress search, you got a 404 response.

As a general rule of thumb, if a parameter is listed in the documentation as valid in a WP_Query post query object, it is unavailable for use in URLs.

Changing WordPress to allow s to be used would cripple a large portion of WordPress core functionality and require extensive internal modification of WordPress itself, as well as disabling all search related functionality, and any plugins that use similar systems, both on the frontend and in WP Admin.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x