簡體   English   中英

使用php顯示curl結果

[英]displaying curl results using php

我是彈性搜索的新手。 我有一個網址。 直接執行該URL時,我得到了結果。 但是當我嘗試使用curl運行時,我沒有得到任何數據。 以下是我的鏈接

http://localhost:9200/bank/_search?q=address:mill

我得到的上述鏈接的樣本響應是

    {"took":1,"timed_out":false,"_shards":     {"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":4.8004513,"hits":[{"_index":"bank","_type":"account","_id":"136","_score":4.8004513,"_source":{"account_number":136,"balance":45801,"firstname":"Winnie","lastname":"Holland","age":38,"gender":"M","address":"198 Mill Lane","employer":"Neteria","email":"winnieholland@neteria.com","city":"Urie","state":"IL"}
}]}}

以下是上面網址的卷曲程序。

$url = "localhost:9200/bank/account/_search?q=mill";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);

您需要使用HTTPGET CURLOPT的選項,然后您需要通過URL添加您的參數。

試試以下代碼:

 <?php
try {
$url = "http://localhost:9200/bank/_search";
$search_val     = 'mill';

$str = "q=address:".$search_val;
$url_final = $url.'?'.$str;

  $ch = curl_init();
 if (FALSE === $ch)
        throw new Exception('failed to initialize');
curl_setopt($ch, CURLOPT_URL, $url_final);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$return = curl_exec ($ch);
if (FALSE === $return)
        throw new Exception(curl_error($ch), curl_errno($ch));
} catch(Exception $e) {

    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);

}
curl_close ($ch);
echo $return;
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM