【问题标题】:Make a search form to search with http api制作一个搜索表单以使用 http api 进行搜索
【发布时间】:2017-02-17 09:06:25
【问题描述】:

我对这件事很陌生。我正在尝试创建一个搜索表单,它允许通过 http api 搜索数据库并在网站上显示结果。调用需要授权:

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

我的搜索表单如下所示:

<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>

当我在输入字段中输入内容并点击提交按钮时,浏览器会显示:

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

在浏览器地址字段中我看到:

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

很明显授权被忽略了,url显示我已经离开了我的网站。

我必须使用 action="somephp.php" 来完成这项工作吗? 我如何从表单授权呼叫并在网站上显示响应? 非常感谢提示。神

【问题讨论】:

    标签: http search


    【解决方案1】:

    感谢德国 wordpress 论坛,我找到了答案—— 表格:

    <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>
    

    然后打电话:

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

    这很好用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 2016-11-14
      • 2021-09-10
      相关资源
      最近更新 更多