dongtuojuan8998 2017-10-29 09:15
浏览 192
已采纳

为什么这个urlencode JSON GET请求不起作用?

$word = $_GET['search'];
$json = json_encode('{"query":{"match":{"name":"'.$word.'"}}}');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://10.128.0.2:9200/testers/test/_search' . urlencode($json));
$resp = curl_exec($curl);

$word is filled from the URL and this is working normally. $resp isn't coming back with any value. 'name' is the name of elasticsearch field.

I am using PHP to run this curl request as per elasticserch except all in one URL. Are there any better ways to do this from PHP code?

  • 写回答

1条回答 默认 最新

  • drr47973 2017-10-29 09:18
    关注

    If you're using a GET call, you need to pass the query in the source query string parameter:

    curl_setopt($curl, CURLOPT_URL, 'http://10.128.0.2:9200/testers/test/_search?source=' . urlencode($json));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?