将带有查询参数的URL转换为guzzle

时间:2017-10-20 13:25:19

标签: php url request guzzle

使用" guzzlehttp / guzzle":" ^ 6.3",如何将包含查询参数的URL转换为使用选项数组的语法?

$exampleUrl = http://A001234/datadownload/test?reference=abc&opt_responseformat=json&opt_servicemode=async

在浏览器或POSTMAN中粘贴这个给我一个不错的小JSON树桩。但我无法使用选项数组。以下是我尝试的一些代码:

$client = new GuzzleHttp\Client([        
    'headers' => [            
        'Accept' => 'application/json',
        'Content-Type' => 'application/json' 
    ],
    'verify' => false
]);

$result = $client->request('GET', 'http://A001234/datadownload/test', [
    "form_params" => [
        "opt_responseformat" => "json",
        "opt_servicemode"=> "async"
    ]
]);
if($result->getStatusCode() == 200) {
    dd(json_decode($result->getBody())); // dumps null expecting json object
}   

我缺少什么想法?

1 个答案:

答案 0 :(得分:2)

试试这个:

$client = new GuzzleHttp\Client([        
    'headers' => [            
        'Accept' => 'application/json',
        'Content-Type' => 'application/json' 
    ],
    'verify' => false
]);

$result = $client->request('GET', 'http://A001234/datadownload/test', [
    "query" => [
        "reference" => "abc",
        "opt_responseformat" => "json",
        "opt_servicemode"=> "async"
    ]
]);
if($result->getStatusCode() == 200) {
    dd(json_decode($result->getBody())); // dumps null expecting json object
}