2017-02-17 59 views
0

我是這個問題的新手。我試圖創建一個搜索表單,它允許通過http api搜索數據庫並在網站上顯示結果。呼叫需要授權:使用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顯示,我已經離開我的網站。

我一定使這項工作與行動=「somephp.php」? 我如何授權來自表單的電話並在網站上顯示回覆? 提示非常感謝。西奧

回答

0

得益於德國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. 

這種運作良好。