Search code examples
phpjsontwiliotwilio-autopilot

How to receive Twilio Autopilot data with PHP


mates!

I have a PHP code that must receive some JSON (x-www-form-urlencoded) data from Twilio Autopilot redirect.

I am using the code below:

$data = file_get_contents('php://input');

The file_get_contents('php://input') is returnig data like below(Does not look like a JSON):

It does not look like a JSON

Does anyone can help me with this?


Solution

  • That is x-www-form-urlencoded data, which is definitely different from JSON data. x-www-form-urlencoded data is made up of key/value pairs that are separated by a = and each pair is separated by an &. For example: CurrentTask=deliver_roomitems&CurrentInput=666

    In PHP you can parse this string with the parse_str method.

    $data = file_get_contents('php://input');
    $parsed_data = parse_str($data, $result);
    echo $result["CurrentTask"]; // => deliver_roomitems
    

    When you receive incoming HTTP requests in PHP, data like this is usually parsed for you into the $_GET, $_POST and $_REQUEST variables. You should find you can also access the data by key in the relevant variables. e.g.

    echo $_REQUEST["CurrentTask"]; // => deliver_roomitems