-3

I have a JSON string that I am working with in PHP. I am currently having trouble grabbing a phone number out of the string as it is nested inside the string farther than I know how to interact.

The phone string could come in as Mobile, Work, or Home so I am trying to work through getting the number regardless, I haven't been successful thus I haven't done the Work handling yet.

Any input on how to better do this?

Example JSON string here:

{
  "event": "Customer Account Created",
  "data": {
    "customerID": "10834",
    "extAccountID": null,
    "customerType": null,
    "companyName": null,
    "salutation": null,
    "firstName": "John",
    "lastName": "Doe4",
    "emailAddress": "john@doe4.com",
    "numbers": [
      {
        "Type": "Mobile",
        "Number": "3305551234"
      }
    ],
    "customerNotes": "Location",
    "referredBy": null,
    "customerSecurityCode": null,
    "accountType": 1,
    "customerGroup": "10",
    "customerPortalUsername": null,
    "customerPortalPassword": null,
    "physicalStreet": "327 West First St",
    "address2": null,
    "physicalCity": "Smithville",
    "physicalState": "OH",
    "physicalZip": "44695",
    "automaticallyGeocode": true,
    "physicalLatitude": "",
    "physicalLongitude": "",
    "billingSameAsPhysical": "1",
    "taxZone": "1",
    "invoicePreference": "Email",
    "invoiceDetailPages": null,
    "paymentMethod": "Cash",
    "nameOnCard": null,
    "creditCardNumber": null,
    "creditCardExpirationMonth": null,
    "creditCardExpirationYear": null,
    "cardType": null,
    "creditCardUseBillingAddress": null,
    "bankAccountNameOnAccount": null,
    "bankAccountRoutingNumber": null,
    "bankAccountAccountNumber": null,
    "bankAccountAccountType": null,
    "prePayMonths": 1,
    "fixedDay": "Bill Cycle Day",
    "billDayFixed": "0",
    "dueBy": "15",
    "graceDaysBillDayFixed": "5",
    "layoutFormat": null
  }
}

My current php script interacting with the JSON is here:

<?php
// Check if the request method is POST

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get the JSON data from the request body
    $jsonString = file_get_contents('php://input');
    
    // Decode the JSON data
    $data = json_decode($jsonString, true);
    
    $event = $data['event'];
$customerID = $data['data']['customerID'];
$customerType = $data['data']['customerType'];
$firstName = $data['data']['firstName'];
$lastName = $data['data']['lastName'];
$emailAddress = $data['data']['emailAddress'];


$phoneNumberHome = $data['data']['numbers']['Home']['Number'];

$phoneNumberMobile = $data['data']['numbers']['Mobile']['Number'];

$variablesString = "Event: $event\nCustomer ID: $customerID\nCustomer Type: $customerType\nFirst Name: $firstName\nLast Name: $lastName\nEmail Address: $emailAddress\nHome Phone Number: $phoneNumberHome\nMobile Phone Number: $phoneNumberMobile";

    if ($data !== null) {
        // Write the JSON data to the variables.txt file
      file_put_contents('variables.txt', $variablesString);
        
        // Send a response indicating success
        http_response_code(200);
        echo 'Data has been written to variables.txt';
    } else {
        // Send a response indicating invalid JSON
        http_response_code(400);
       $stringtoinsert = 'Invalid JSON data';
    }
} else {
    // Send a response for unsupported request methods
    echo 'Method Not Allowed';
}
?>


  • 1
    `$data['data']['numbers']` doesn't have a Home or Mobile name/key. If you look, it has an array of objects, and each object has a `Type` that is Mobile or Home or whatever else, and a `Number` with the phone number. So iterate through `$data['data']['numbers']`, and then check the Type before adding it to the variable you want. – aynber Aug 31 '23 at 18:02

0 Answers0