簡體   English   中英

Guzzle POST 返回 GuzzleHttp\\Exception\\ClientException 客戶端錯誤:`POST

[英]Guzzle POST returning GuzzleHttp\Exception\ClientException Client error: `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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM