透過 php 顯示 API 的過濾結果 - BatchData
P粉014293738
P粉014293738 2023-09-09 21:10:17
0
1
528

整合批次資料 API 並在僅顯示已過濾項目之一時出現問題。 api範例是:https://developer.batchdata.com/docs/batchdata/batchdata-v1/operations/create-a-property-skip-trace

在此範例中,我嘗試僅顯示電話號碼或電子郵件欄位。顯示完整的結果,所以我知道 api 正在工作...但是,我無法顯示過濾後的項目,如下所示:

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

這是我現在擁有的完整程式碼:

<?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

全部回覆(1)
P粉187677012

在嘗試存取欄位之前,您需要先解碼 JSON

在您的 else 語句中加入此內容

$decodedResponse = json_decode($response);

我不確定您返回的確切JSON 回應是什麼,但請查看示例,您可能需要使用它來獲取電子郵件和電話號碼,但如果沒有實際回應,我無法確認這一點(如果包含電子郵件和電話號碼,請勿發佈您的實際回覆)

$email = $decodedResponse->results->persons[0]->emails[0]->email;
$phoneNumber = $decodedResponse->results->persons[0]->phoneNumbers[0]->number;
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!