0

I am making a request to an API using its post.rest web service. I have constructed the request using Postman. I get the error message, "Parameter 'query' is required, and it must have a value." I have been trying to work this out for several days, and I'm at my wits' end. I thought I could resolve it myself, but have not been successful. It seems like I've tried at least a hundred different permutations of the code.

Here is my PHP code for the request:

     <?php
     $curl = curl_init();

     curl_setopt_array($curl, array(
     CURLOPT_URL => "https://SDMDataAccess.sc.egov.usda.gov/Tabular/post.rest",
     CURLOPT_RETURNTRANSFER => true,
     CURLOPT_ENCODING => "",
     CURLOPT_MAXREDIRS => 10,
     CURLOPT_TIMEOUT => 30,
     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
     CURLOPT_CUSTOMREQUEST => "POST",
     CURLOPT_POSTFIELDS => 
        $data = array("query" => "SELECT l.areasymbol, l.areaname, l.lkey, 
          musym, muname, museq, mu.mukey
          FROM sacatalog sac 
          INNER JOIN legend l ON l.areasymbol = sac.areasymbol 
          AND l.areatypename = 'Non-MLRA Soil Survey Area' 
          INNER JOIN mapunit mu ON mu.lkey = l.lkey 
          AND mu.mukey IN (455997) 
          ", "FORMAT" => "JSON"),
     CURLOPT_HTTPHEADER => array(
        "Cache-Control: no-cache",
        "Content-Type: application/json",
        "Postman-Token: 520636bc-a062-4ab8-99e7-7edaae5118b4"
      ),
    ));

    file_put_contents('soil_request.txt', $data);
    $response = curl_exec($curl);

    $err = curl_error($curl);

    curl_close($curl);

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

I am unable to find any examples of code for submitting rest requests to this API service in their documentation or elsewhere on the internet, even though their documentation is vast.

Thank you in advance for your consideration on this. I really appreciate it.

5
  • 1
    From en.1answer.info/676973-7a313939383734 it looks like you may need to change your data array into a json string before sending it Commented May 27, 2018 at 0:05
  • @ivanivan Okay. I see from that post that the following code might do it. $data_string = json_encode($data); But I hvae no idea where I need to insert it. Commented May 27, 2018 at 0:12
  • 1
    Try $data = json_encode($data); Commented May 27, 2018 at 0:58
  • @ivanivan Thank you for that. It worked in getting a result. But now the result is giving me a string that isn't responding to json_decode(). The result is supposed to be an array, but isn't. This is a problem I was having earlier, and I thought I could get around it by revising the request. Commented May 27, 2018 at 1:39
  • I've edited my answer with working code. Commented May 27, 2018 at 2:13

1 Answer 1

1

From https://en.1answer.info/676973-7a313939383734 it looks like you may need to change your data array into a json string before sending it

Also, your line that is

CURLOPT_POSTFIELDS=>$data=array("select ....

is wrong.

Instead, define your data array, then build a http query string out of it. Also, the parameter names are case sensitive, so you want to use query instead of QUERY and format instead of FORMAT.

Working code -

 <?php

    $data["query"]="SELECT l.areasymbol, l.areaname, l.lkey,
          musym, muname, museq, mu.mukey
          FROM sacatalog sac
          INNER JOIN legend l ON l.areasymbol = sac.areasymbol
          AND l.areatypename = 'Non-MLRA Soil Survey Area'
          INNER JOIN mapunit mu ON mu.lkey = l.lkey
          AND mu.mukey IN (455997)";

     $data["format"] = "JSON";

     $curl = curl_init();

     curl_setopt_array($curl, array(
     CURLOPT_URL => "https://SDMDataAccess.sc.egov.usda.gov/Tabular/post.rest",
     CURLOPT_RETURNTRANSFER => true,
     CURLOPT_ENCODING => "",
     CURLOPT_MAXREDIRS => 10,
     CURLOPT_TIMEOUT => 30,
     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
     CURLOPT_CUSTOMREQUEST => "POST",
     CURLOPT_POSTFIELDS => http_build_query($data),
     CURLOPT_HTTPHEADER => array(
        "Cache-Control: no-cache",
        "Content-Type: application/json",
        "Postman-Token: 520636bc-a062-4ab8-99e7-7edaae5118b4"
      ),
    ));

    file_put_contents('soil_request.txt', $data);
    $response = curl_exec($curl);

    $err = curl_error($curl);

    curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This looks great, but I have to stop for the day. I'll be able to get back to it in the morning, and try these things you suggest. Hopefully, this will do it.
This is perfect! Thank you! Very, very satisfying to finally have it working!

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.