php - 使用PHP显示curl结果

标签 php curl elasticsearch

我是 flex 搜索的新手。我有一个网址。直接在执行该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的curl程序。
$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);

最佳答案

您需要使用 CURLOPT HTTPGET 选项,然后需要在参数后附加网址。

试试下面的代码:

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

关于php - 使用PHP显示curl结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40415802/

相关文章:

php - MySQLi 和 Phalanger

php - 如何使用 stdclass 对象从数组中获取值?

php - 在 Elasticsearch 和 Symfony2 中处理日期范围

elasticsearch - 如何使用 elasticsearch python 客户端创建连接关系

elasticsearch - 在 Elasticsearch 中查找连续有一个字符的次数

php - MySQL - 按日期选择行并传递给 php 数组

php - 创建博客但 mysql php 密码问题

javascript - 在 react-native 中通过 PUT 方法上传文件

php - $_POST 值获取加号删除

python - Pycurl:上传具有 UTF-8 文件名的文件