2016-08-31 32 views
1

我對Web開發瞭解不多,並且正在努力構建簡單的搜索頁面。所有搜索引擎編碼都是功能性的,並且將通過REST API進行調用。使用REST API搜索輸入的文本(HTML + PHP)

我的HTML網頁是這一個:

<div> 
    <div> 
    <input id="search_query" type="text" placeholder="Insert the text" /> 
    </div> 
    <div><button id="search" type="submit">Go</button></div> 
</div> 

<div id="results"> </div> 

而且PHP調用像這樣做:

<?php 
require_once 'unirest-php/lib/Unirest.php'; 
$response = Unirest\Request::post(
"MY_URL", 
    array(
     "X-Domino-Api-Key" => "MY_API_KEY", 
     "Content-Type" => "application/json" 
    ), 
json_encode(array("parameters" => [ "THE_QUERY" ])) 
); 

print_r($response->code); 
print_r($response->headers); 
print_r($response->body); 
?> 

我就死在如何將其鏈接,如何從得到查詢輸入以及如何將結果放在正確的位置。

也許這很容易,但我真的沒有web開發經驗,所以我希望能夠保持簡單的解決方案。

謝謝!

+0

哪來正確的地方? –

+0

它將在div「結果」 – David

回答

0

Google「HTML表單」。您的搜索表單缺少表格部分。下面是HTML,假設你的PHP文件名爲 「的search.php」:

<form action="search.php" method="GET" target="someFrame"> 
    <input name="search_query" type="text"> 
    <input type="submit"> 
</form> 

<iframe name="someFrame"> 
    <!--results will appear in here--> 
</iframe> 

而在你的PHP,你將需要改變:

json_encode(array("parameters" => [ "THE_QUERY" ])) 

json_encode(array("parameters" => $_GET["search_query"])) 
+0

嗨艾倫,謝謝你的答案。但是我仍然看不到從REST API調用的結果。看起來,當GET被調用時,它會檢索到「找不到頁面」錯誤......任何想法爲什麼會發生這種情況?當我在git集線器上測試API調用時,我可以檢索結果,但不能將它們放在網頁上。 – David

+0

確保'action =「search.php」'中的路徑是正確的 - 在我給出的代碼中,我假設你的HTML文件與「search.php」文件位於同一目錄中。您可能還想將「search.php」的內容更改爲「echo'hello world';'現在確保它可以正常工作。 –

+0

嗨,它的工作,但我得到了一個錯誤消息:「警告:輸入中的意外字符:'\'(ASCII = 92)狀態= 1在/ home/a3553542/public_html/search.php第3行」。也許我使用的服務器不支持從我的API中檢索到的字符(我用它的葡萄牙語,帶有重音和'ç') – David