您好我正在使用radchet websocket。我很难从对象变量中获取数据。
请检查我的代码:
var conn = new WebSocket('ws://localhost:8080?user_id=10&receiver_id=20');
$querystring = $conn->WebSocket->request->getQuery();
print_r($querystring);
输出:
Guzzle\Http\QueryString Object
(
[fieldSeparator:protected] => &
[valueSeparator:protected] => =
[urlEncode:protected] => RFC 3986
[aggregator:protected] =>
[data:protected] => Array
(
[user_id] => 10
[receiver_id] => 20
)
)
以上代码我想要user_id
和receiver_id
,但我无法获得。
我的代码:
echo $querystring->data:protected['user_id'];
echo $querystring->data:protected['receiver_id'];
我已经回应但收到错误消息。请帮帮我。
编辑:
如果我将对象转换为数组,则表示格式不正确。见下文。
$array = (array) $querystring;
print_r($array);
输出:
Array
(
[ * fieldSeparator] => &
[ * valueSeparator] => =
[ * urlEncode] => RFC 3986
[ * aggregator] =>
[ * data] => Array
(
[user_id] => 10
[receiver_id] => 20
)
)
答案 0 :(得分:6)
Guzzle\Http\QueryString扩展Guzzle\Common\Collection,以便您可以使用Collection的方法:
$user_id = $querystring->get('user_id');
$receiver_id = $querystring->get('receiver_id');
或
$parameters = $querystring->toArray();
$user_id = $parameters['user_id'];
$receiver_id = $parameters['receiver_id'];