Search code examples
phpguzzleratchet

How to access Guzzle QueryString parameters in Ratchet WebSockets?


Hi I am using radchet websocket. I am getting trouble to get my data from an object variable.

Please check my code:

var conn = new WebSocket('ws://localhost:8080?user_id=10&receiver_id=20');
$querystring = $conn->WebSocket->request->getQuery();
print_r($querystring);

Output:

Guzzle\Http\QueryString Object
(
    [fieldSeparator:protected] => &
    [valueSeparator:protected] => =
    [urlEncode:protected] => RFC 3986
    [aggregator:protected] =>
    [data:protected] => Array
        (
            [user_id] => 10
            [receiver_id] => 20
        )
)

Above code i want user_id and receiver_id but i am unable to get.

My code:

echo $querystring->data:protected['user_id'];
echo $querystring->data:protected['receiver_id'];

I have echoed but getting error message. Please help me.

EDIT:

If I convert object to array it is not showing proper format. see below.

$array =  (array)  $querystring;
print_r($array);

Output:

Array
(
    [ * fieldSeparator] => &
    [ * valueSeparator] => =
    [ * urlEncode] => RFC 3986
    [ * aggregator] =>
    [ * data] => Array
        (
            [user_id] => 10
            [receiver_id] => 20
        )

)

Solution

  • Guzzle\Http\QueryString extends Guzzle\Common\Collection so you should be able to use Collection's methods:

    $user_id = $querystring->get('user_id');
    $receiver_id = $querystring->get('receiver_id');
    

    or

    $parameters = $querystring->toArray();
    $user_id = $parameters['user_id'];
    $receiver_id = $parameters['receiver_id'];