php - Guzzle POST 返回 GuzzleHttp\Exception\ClientException 客户端错误:`POST
问题描述
大家下午好;
几天来一直试图解决这个问题,像往常一样,这是在尝试从有类似错误的人那里寻找解决方案之后的最后手段。
我正在尝试通过 xml 发布到 API 并出现以下错误
GuzzleHttp\Exception\ClientException
Client error: `POST https://go.paytraq.com/api/shipper?APIToken=xxxxxxx&APIKey=xxxx` resulted in
a `400 Bad Request` response: <!DOCTYPE html> <html> <head> <title>PayTraq - Cloud-based
Business Suite - Manage your business online</ti (truncated...)
由于错误被截断,试图找到最好的方法来获得完整的错误,但没有成功。
请在下面找到我的代码..
$array = [
'ShipperName'=>[
'_value'=>$shipper_name
],
'ShipperVehicle'=>[
'_value'=>$shipper_vehicle
],
'ShipperDriver'=>[
'_value'=>$shipper_driver
],
'ShipperRegNumber'=>[
'_value'=>$shipper_reg
],
'IsDefault'=>[],
'IsInactive'=>[]
];
$xmlarray = ArrayToXml::convert($array,'Shipper');
try {
$client = new \GuzzleHttp\Client(['base_uri'=>'https://go.paytraq.com']);
$detail = $client->request(
'POST',
'/api/shipper',
['debug'=>'false',/*'debug'=>'false',this is debug line*/
'query'=>[
'APIToken'=>'xxx',
'APIKey'=>'xxxxxx'
],
'headers' => [
'Content-Type' => 'text/xml;',
],
['body' => $xmlarray]
]);
$response = $client->post($detail);
} catch (\GuzzleHttp\Exception\ClientErrorResponseException $e) {
var_dump($e->getResponse()->getBody()->getContents());
}
return redirect()->back();
下面是使用 echo $xmlarray 得到的结果
<?xml version="1.0"?>
<Shipper>
<ShipperName>
test
</ShipperName>
<ShipperVehicle>
test
</ShipperVehicle>
<ShipperDriver>
test
</ShipperDriver>
<ShipperRegNumber>
test
</ShipperRegNumber>
<IsDefault/>
<IsInactive/>
</Shipper>
Guzzle调试如下
* Trying 52.16.7.7:443... * Connected to go.paytraq.com (52.16.7.7) port 443 (#0) * ALPN,
offering http/1.1 * successfully set certificate verify locations: * CAfile:
/usr/local/etc/openssl@1.1/cert.pem CApath: /usr/local/etc/openssl@1.1/certs * SSL connection
using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256 * ALPN, server did not agree to a protocol * Server
certificate: * subject: CN=go.paytraq.com * start date: Oct 14 00:00:00 2019 GMT * expire date:
Nov 14 12:00:00 2020 GMT * subjectAltName: host "go.paytraq.com" matched cert's "go.paytraq.com"
* issuer: C=US; O=Amazon; OU=Server CA 1B; CN=Amazon * SSL certificate verify ok. > POST
/api/shipper?APIToken=xxxx&APIKey=xxxxx HTTP/1.1 Host: go.paytraq.com Content-Length: 0 User-
Agent: GuzzleHttp/7 Content-Type: text/xml; * Mark bundle as not supporting multiuse < HTTP/1.1
400 Bad Request < Content-Type: text/html; charset=utf-8 < Date: Sun, 23 Aug 2020 14:26:09 GMT <
Request-Time: 3 < Server: nginx/1.4.6 (Ubuntu) < Content-Length: 3835 < Connection: keep-alive <
* Connection #0 to host go.paytraq.com left intact
从网站这是需要的
The transmission of all API requests and responses needs to be made over HTTPS.
There are two type of requests: GET and POST. GET requests are usually used to read the data, POST request are used to add and update the data.
All POST requests should be made in XML format with "Content-Type: text/xml" header.
All success responses are also given in XML format.
400 Bad Request The request could not be understood by the server due to malformed syntax, invalid values or validation issues.
<Shipper>
<ShipperName></ShipperName>
<ShipperRegNumber></ShipperRegNumber>
<ShipperVehicle></ShipperVehicle>
<ShipperDriver></ShipperDriver>
<IsDefault></IsDefault>
<IsInactive></IsInactive>
</Shipper>
Only <ShipperName> is required.
提前谢谢大家
萨基
解决方案
好吧终于让它工作了
这就是我必须做的修改才能让它工作。
将来可能会帮助某人。
$client = new \GuzzleHttp\Client(['base_uri' =>'https://go.paytraq.com']);
$detail = $client->post(
'/api/shipper',
[/*'debug'=>'false',this is debug line*/
'headers' => [
'Content-Type' => 'text/xml',
],
'body' => $xmlarray,
'query'=>[
'APIToken'=>'3GUoyOxsCznhyiFQ',
'APIKey'=>'6421f7ad-418b-46b0-891b-e4385071a88f-7608'
],
]);
$response = $detail->getStatusCode();
推荐阅读
- redis - 在redis中为不存在的键设置多个键的选项?
- python - 如果模型实例字段等于 inplay (Django),如何运行函数
- javascript - Firebase 云函数查询不起作用
- sql - SQL Server:更改日期格式
- python - 在 Python 中格式化 JSON 输出
- mysql - 如何以相反的顺序插入行?
- angular7 - 在angular7中构建库时aws sdk无法正常工作
- c# - 如何使用 C# 访问 Windows 音量混合器?
- mongodb - MongoDB updateMany 不是一个函数
- django - 尝试创建自定义 http 错误时语法无效