1

i am new to this matter. I am trying to create a search form, that allows searching a database via http api and display the result on a website. The call requires authorization:

$headers = array ('headers' => array( 
                  'Authorization' =>       'bearer' . $token  ,
                  'Content-type'  =>       'application/json',
                  'Accept'        =>       'application/json'));

My search form looks like this:

<form method="GET" accept="application/json" action="https://example.xx/api/v1/products?page=1&size=5&direction=asc&search=value">
  <input type="text" name="search" size="40" maxlength="256" value="" placeholder="testsearch">
  <input type="submit" name="search_button" value="Search">
</form>

When i enter something into the input field and hit the submit button, the browser displays:

{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}

And in the browsers address field i see:

https://example.xx/api/v1/products?search=testvalue&search_button=Search

Obviously the authorisation is ignored and the url shows that i have left my website.

Do i have to make this work with an action="somephp.php"? How can i authorize the call from the form and display the response in a website? Hints much appreciated. theo

1 Answer 1

1

Thanks to the german wordpress forum, i found the answer – The Form:

<form method="post" id="api-result-searchform" action="">
    <input type="text" class="search" placeholder="<?php echo esc_attr_x( 'Author?', 'placeholder' ) ?>" value="" name="searchauthor" id="s" title="api-search" />
    <input class="button"type="submit" name="search_button" value="Search">
</form>

And the call:

<?php
    $searchterm = $_POST['searchauthor'];
    $url = 'https://example.de/api/v1/product?search=ti=' . $searchterm;
    $response = wp_remote_get($url, $headers);
    //etc.

This works well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.