Update the segment and create a contact from the script

The Mautic Dev documentation is your friend here:
https://developer.mautic.org/#create-contact

  1. First enable access (Basic Auth) in Mautic.
  2. Add a user for the API with required access rights.
  3. Implement the calls: You need to check first, if contact already exists, if not add a new one.

Not sure what you mean related to the assignment of the contact to a segment - that would be the way to go: Mautic Developer Documentation