【问题标题】:Convert URL with query parameters to guzzle将带有查询参数的 URL 转换为 guzzle
【发布时间】:2018-04-01 16:24:12
【问题描述】:

使用“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
}   

任何想法我错过了什么?

【问题讨论】:

    标签: php url request guzzle


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-24
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      • 2015-04-02
      • 1970-01-01
      相关资源
      最近更新 更多