-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
  • 2
    Build a PHP array and json_encode it instead of building a JSON string by hand! Commented Aug 11, 2022 at 6:44

1 Answer 1

-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));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.