How to access Guzzle QueryString parameters in Ratchet WebSockets?

£可爱£侵袭症+ 提交于 2020-01-16 03:38:05

问题


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
        )

)

回答1:


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'];


来源:https://stackoverflow.com/questions/29965445/how-to-access-guzzle-querystring-parameters-in-ratchet-websockets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!