1

I wrote a query through 'postman' which looks like this

POST: http://someurl  

{
    "elasticQuery": //Elastic Query is a user defined field.
    {
        "_source": [ "field 1", "field 2", "field 3" ],
        "query": 
        {
            "match_all":{}
        }
    }, 
    "index": "indexName", 
    "scroll": "True"
}

This returns lots of JSON formatted documents from the site with 3 given fields. I am wondering how I would format this query using PHP to do a curl call and recieve the same documents.

I am not sure if this is even possible but your help will be greatly appreciated. Thanks.

1 Answer 1

2

Try the following:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "YOUR_URL_HERE",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"elasticQuery\":{\"_source\":[\"field 1\",\"field 2\",\"field 3\"],\"query\":{\"match_all\":{}}},\"index\":\"indexName\",\"scroll\":\"True\"}",
  CURLOPT_HTTPHEADER => array(
    "accept: application/json",
    "cache-control: no-cache",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Remember to replace the CURLOPT_URL string with your own URL. As a further tip, you can get Postman to generate this for you. With the request open in a tab, towards the top right below the "Send" button, there's a small bit of text which says "Code". Select it and you should have the option to generate PHP cURL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.