Display filtered results from API via php - BatchData
P粉014293738
P粉014293738 2023-09-09 21:10:17
0
1
614

Integrating the Batch Data API and having issues showing only one of the filtered items. API example is: https://developer.batchdata.com/docs/batchdata/batchdata-v1/operations/create-a-property-skip-trace

In this example I am trying to display only the phone number or email field. Showing the full results so I know the api is working... However, I can't show the filtered items like this:

<?php $email; ?>
<?php echo $response->response->results->result[0]->results->persons->email;?>
<?php echo $response->options->customProjection[0]->results->persons->phoneNumbers->number;?>

This is the complete code I have now:

<?php
$location = $entity->getSingleFieldValue('field_site_address');
$street = $location['street'];
$city = $location['city'];
$state = $location['province'];
$email = $response->response->results->result[0]->results->persons->email;

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.batchdata.com/api/v1/property/skip-trace",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\n  \"requests\": [\n    {\n
      \"propertyAddress\": {
        \"street\": \"$street\",
        \"city\": \"$city\",
        \"state\": \"$state\"
      }   }\n  ]\n}",
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer xxxxxx",
    "Content-Type: application/json"
  ],
]);

$data = simplexml_load_string($result);
$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

?>

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


<?php $email; ?>
<?php echo $response->response->results->result[0]->results->persons->email;?>
<?php echo $response->options->customProjection[0]->results->persons->phoneNumbers->number;?>

P粉014293738
P粉014293738

reply all(1)
P粉187677012

You need to decode the JSON before trying to access the fields

Add this in your else statement

$decodedResponse = json_decode($response);

I'm not sure what the exact JSON response you are returning is, but looking at the example, you may need to use this to get the email and phone number, but I can't confirm this without an actual response (if the electron is included Email and phone number, please do not post your actual response)

$email = $decodedResponse->results->persons[0]->emails[0]->email;
$phoneNumber = $decodedResponse->results->persons[0]->phoneNumbers[0]->number;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template