0

Using "guzzlehttp/guzzle": "^6.3", how can I convert a URL including query parameters to a syntax where an options array is used?

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

Pasting this in browser or POSTMAN gives me a nice little JSON stump back. But I have not been able to use this with an options array. Here is some code, that I tried:

$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
}   

Any ideas what I am missing?

1 Answer 1

3

Try this:

$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
}  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.