-2

i'm trying to fetch elastic api with php fetch, but on the query or the body i'm using one quote to create the query, i know that variable in php will be readed by php if we use double quote but unfortunately elastic query only accept double quote as the format of the query, so i use the one quote as the opener of the string for query, and the variable won't readed by php as varible, is it has alternative so i the php can read the variable as a variable and not as a text

PHP query elastic

    $nameCategoryListSub = "Laptop";
    $body = '{
      "query" : {
        "match": {
          "category": "$nameCategoryListSub"
        }
      }
    }';

enter image description here

1 Answers1

-1

Use PHP associative array to build your query and json_encode it to get the json query in result.

Here is the code to do that -

$nameCategoryListSub = "Laptop";

$body = [
           "query" => [
              "match" => [
                  "category" => $nameCategoryListSub
               ]
           ]
        ];
print_r(json_encode($body));
Aman Mehta
  • 303
  • 1
  • 12