AnsweredAssumed Answered

FM Data API Find Result

Question asked by danjamins on Dec 24, 2018
Latest reply on Dec 24, 2018 by danjamins

Hello,

 

I'm using PHP to work with the data API and trying to perform a find on a specific field in a table. Everything works fine and it is finding the record but the response is of type "object" and using json_decode() on the response data does not convert it to a PHP array as expected. Any idea on what is going wrong here? It does not do this using practically the same code but a get request to get a list of data and decode it. That I am able to parse and display easily.

 

Here is the PHP:

<?php
$token = '11241cc12f3b57c7b8776d6a149d8196ecc9fff210c392f7646';

// build the find query
$query = array(
  'query' => array ( array ( "lead_id" => 415747))
);
$query = json_encode($query);

// send the query and get the result
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://myserver.com/fmi/data/v1/databases/my_database/layouts/leads_dp/_find",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "$query",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer " . $token,
    "Content-Type: application/json"
  ),
));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURL_OPT_SSL_VERIFYHOST, FALSE);

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

// should convert to php array but something goes wrong
$response = json_decode($response);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  print_r($response);
  echo("<br/><br/>Type: " . gettype($response) . "<br/>Size: " . sizeof($response));
}
?>

 

And here is the result of this:

stdClass Object ( [response] => stdClass Object ( [data] => Array ( [0] => stdClass Object ( [fieldData] => stdClass Object ( [lead_id] => 415747 [First_Name] => John [Last_Name] => Doe [Phone_Primary] => 5554447777 [Phone_Alt] => [Second_Name] => [Address] => 123 Fake St [City] => Beverly Hills [State] => CA [Zip] => 90210 [home_UtilityProvider] => [home_HOA] => [home_HOAName] => [home_MountType] => [createdTimestamp] => 12/24/2018 10:09:52 [Created_By] => sun [Modified_By] => sun [modifiedTimestamp] => 12/24/2018 10:10:16 [Email] => [Email_2] => [home_UtilityAccountNumber] => [home_MeterNumber] => [home_ServiceAgreementNumber] => ) [portalData] => stdClass Object ( ) [recordId] => 240582 [modId] => 0 ) ) ) [messages] => Array ( [0] => stdClass Object ( [code] => 0 [message] => OK ) ) )

 

Type: object

Size: 1

 

Any idea why the response is like a halfway formatted PHP array but returns as type "object" and when trying to parse through it the page says it is not working or it goes blank when using get_object_vars on any level further than "response"

Outcomes