Lori
Lori

Reputation: 59

REST API request in PHP - query error

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.

Upvotes: 0

Views: 308

Answers (1)

ivanivan
ivanivan

Reputation: 2215

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;
}

Upvotes: 1

Related Questions