Custom forwarding solution with twilio

I'm new to twilio and I'm struggling with one task. I want to do the following

User A calls a twilio number
User A is asked to type in a specific ID which he got assigned already beforehand by mail
Twilio requests the URL www.mycompany.com/api/twilio/get_recipient_number.php?id=[here the typed in ID] to get the phone number of the recipient
Twilio forwards the caller to the phone number which was provided by the script

Is it possible to do this with twilio? What would be the best way?

Twilio employee here.
Yes, this is possible with Twilio. However you have to do step 3 slightly differently.
Allow me to explain the process step by step:

User A calls your Twilio phone number.
The phone number requests some TwiML that uses the < Gather > tag to gather the digits User A types. You will want to use some TwiML similar to this:

<Response>
    <Gather action="/route_after_input.php" finishOnKey="*">
Please enter your 4 digit code and press enter
    </Gather>
</Response>

The action url will be called once the * key is pressed. At this URL (do change it from the example above to suit your needs) you can then get the digits type in by inspecting the incoming Twilio request parameter called "Digits"
Use your own code to route the call correctly and return the correct TwiML response.