【发布时间】:2020-11-08 23:13:28
【问题描述】:
我需要将此正文作为 JSON 发送,并使用该标头进行身份验证。
$headers = array(
'Authorization' => "Bearer ".$token->access_token,
'Content-Type' => 'application/json',
);
$body = [
'code1' => '444444',
'code2' => 'zzzzz'
];
第一种方法,此代码不起作用:
$myhttp = new Client();
$res = $myhttp->request('POST', 'https://www.xxxxxxxxxxxx',
[
'headers' => ['Content-Type' => 'application/json',"Authorization" => "Bearer ".$token->access_token],
'json' => ['code1' => '444444','code2' => 'zzzz']
]
);
第二种方法都没有:
$body= json_encode($body);
$res = $myhttp->request('POST', 'https://www.xxxxxxxxxxxx',
[
'headers' => ['Content-Type' => 'application/json',"Authorization" => "Bearer ".$token->access_token],
'body' => $body]
]
);
第三种方法都没有:
当尝试使用 body 时,Guzzle 告诉我这已被弃用,现在使用 "form_params",但 form_params 只接受数组,而不接受 json。
$body= json_encode($body); // encode to json!
$res = $myhttp->request('POST', 'https://www.xxxxxxxxxxxx',
[
'headers' => ['Content-Type' => 'application/json',"Authorization" => "Bearer ".$token->access_token],
'form_params' => $body]
]
);
【问题讨论】: