Guzzle POST返回GuzzleHttp \ Exception \ ClientException客户端错误:`POST

时间:2020-08-25 21:18:30

标签: php laravel guzzle

大家下午好;

尝试解决这一问题已有几天,并且像往常一样,这是尝试从存在类似错误的人那里寻求解决方案之后的最后一招。

我正在尝试通过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>

枪口调试在下面

* 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. 

提前感谢大家

Sarky

1 个答案:

答案 0 :(得分:0)

终于可以正常工作了

那是我必须进行的修改才能使其生效。

将来可能会帮助某人。

 {
    "name": "student",
    "age" : 17,
    "email": "student@example.com", //this is also part of the _id for this document
    "parents": ["parent1@example.com", "parent2@example.com", "teacher2@example.com"],
    "teachers": ["teacher1@example.com", "teacher2@example.com"],
    "parentOf": [],
    "teacherOf": []
}
相关问题