Guzzle POST 请求 php 对比 Ajax
Guzzle POST request php vs Ajax
我的请求在 ajax 上运行良好,但当我尝试 re-create guzzle 场景时失败了。我得到 400 错误的回复。我有一种感觉,我可能在 guzzle 参数中设置了错误?这是我第一次使用这个。
Ajax
var usernameV5 = '108357166';
var passwordV5 = '1234';
var data = {
'type':'base64',
'value':btoa(usernameV5 + ":" + passwordV5),
'namespace':'https://somelink/customers'
};
data = JSON.stringify(data);
$.ajax({
'type':'POST',
'headers':{'Content-Type':'application/json'},
'url':'cantshow.com',
'data':data,
'success':function (result) {
},
'error':function () {
}
});
Guzzle
$username = '108357166';
$password = '1234';
$client = new \GuzzleHttp\Client();
$result = $client->post('cantshow.com', [
'headers' => [
'Content-Type' => 'application/json'
],
'data' => [
'type' => 'base64',
'value' => base64_encode("'".$username.":".$password."'"),
'namespace' => 'https://somelink/customers'
]
])->getBody();
取决于所使用的 guzzle 版本,对于 5 和 6 这应该有效:
$client->post('cantshow.com', array(
'headers'=>array('Content-Type'=>'application/json'),
'json' => array(
'type' => 'base64',
'value' => base64_encode("'".$username.":".$password."'"),
'namespace' => 'https://somelink/customers'
)
));
我的请求在 ajax 上运行良好,但当我尝试 re-create guzzle 场景时失败了。我得到 400 错误的回复。我有一种感觉,我可能在 guzzle 参数中设置了错误?这是我第一次使用这个。
Ajax
var usernameV5 = '108357166';
var passwordV5 = '1234';
var data = {
'type':'base64',
'value':btoa(usernameV5 + ":" + passwordV5),
'namespace':'https://somelink/customers'
};
data = JSON.stringify(data);
$.ajax({
'type':'POST',
'headers':{'Content-Type':'application/json'},
'url':'cantshow.com',
'data':data,
'success':function (result) {
},
'error':function () {
}
});
Guzzle
$username = '108357166';
$password = '1234';
$client = new \GuzzleHttp\Client();
$result = $client->post('cantshow.com', [
'headers' => [
'Content-Type' => 'application/json'
],
'data' => [
'type' => 'base64',
'value' => base64_encode("'".$username.":".$password."'"),
'namespace' => 'https://somelink/customers'
]
])->getBody();
取决于所使用的 guzzle 版本,对于 5 和 6 这应该有效:
$client->post('cantshow.com', array(
'headers'=>array('Content-Type'=>'application/json'),
'json' => array(
'type' => 'base64',
'value' => base64_encode("'".$username.":".$password."'"),
'namespace' => 'https://somelink/customers'
)
));