form_params中的编码美元符号导致$ top,$ skip和$ skipToken被忽略

时间:2019-05-06 22:30:46

标签: php azure guzzle guzzlehttp

通过Azure资源图API访问虚拟机详细信息时,我正在尝试实现分页。当我使用$ top和$ skip时,我仍然会获得所有结果,就像未设置这些选项一样。我最好的猜测是两个变量中的美元符号都已编码,这就是Azure API忽略它们的原因。当我使用PHPStorm的HTTP客户端时,会得到想要的结果。

$client = new GuzzleHttp\Client();
$response = $client->request(
    'POST',
    'https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2019-04-01',
    [
        'headers' => [
            'Accept' => 'application/json',
            'Authorization' => "Bearer {$authorization_token}",
        ],
        'form_params' => [
            'query'         => "where type =~ 'Microsoft.Compute/VirtualMachines' | project id, name, location, resourceGroup, tags, vmId=properties.vmId, vmSize=properties.hardwareProfile.vmSize, networkInterfaces=properties.networkProfile.networkInterfaces",
            'options'       => [
                '$top'  => 25,
                '$skip' => 0,
            ],
            'subscriptions' => [
                $subscription_id,
            ]
        ],
    ]
);

我是否有更好的方法将这些详细信息传递给Guzzle,以便不对那些特定变量进行编码?

编辑: 添加PHPStorm HTTP Client详细信息:

POST https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2019-04-01
Cache-Control: no-cache
Accept: application/json
Authorization: Bearer <token>
Content-Type: application/json

{
  "subscriptions": [
    "<subscription>"
  ],
  "query": "where type =~ 'Microsoft.Compute/VirtualMachines' | project id, name, location, resourceGroup, tags, vmId=properties.vmId, vmSize=properties.hardwareProfile.vmSize, networkInterfaces=properties.networkProfile.networkInterfaces",
  "options": {
    "$top": 25,
    "$skip": 0
  }
}

1 个答案:

答案 0 :(得分:0)

感谢sammitch,更正后的代码为:

$client = new GuzzleHttp\Client();
$response = $client->request(
    'POST',
    'https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2019-04-01',
    [
        'headers' => [
            'Accept' => 'application/json',
            'Authorization' => "Bearer {$authorization_token}",
        ],
        'json' => [
            'query'         => "where type =~ 'Microsoft.Compute/VirtualMachines' | project id, name, location, resourceGroup, tags, vmId=properties.vmId, vmSize=properties.hardwareProfile.vmSize, networkInterfaces=properties.networkProfile.networkInterfaces",
            'options'       => [
                '$top'  => 25,
                '$skip' => 0,
            ],
            'subscriptions' => [
                $subscription_id,
            ]
        ],
    ]
);