当我使用请求模拟邮递员请求时,客户端发送的请求在语法上是不正确的

时间:2018-11-30 12:23:13

标签: php http httprequest

如何使用请求模仿邮递员的http请求?

标题: enter image description here

身体: enter image description here

代码为:

标题:

Authorization: Bearer 123456
Content-Type: application/json

身体:

{
    "amount":"12",
    "currency":"USD",
    "reference":"20174647312238437828",
    "ipn_url":"http://website.com/ipn"
}

我使用上级邮递员请求成功。 但是当我使用Requests模仿邮递员的请求时,我得到了错误:

    $url = QRCODE_URL;

    $data = array(
        'amount'=> 12,
        'currency'=>'USD',
        'reference'=>'123456765432',
        'ipn_url'=>'http://website.com/ipn'
    );
    // $token
    $headers = array("Authorization"=>"Bearer 123456", "Content-Type"=>"application/json");
    $options = $data;

    $request = Requests::post($url, $headers, $data);

    echo '<pre>';
    print_r($request);

我收到这样的错误:

HTTP Status 400 - 
type Status report

message 

description The request sent by the client was syntactically incorrect.

1 个答案:

答案 0 :(得分:0)

我看到您没有将数据编码为JSON,所以我尝试了以下方法:

$url = "https://api.hantepay.com/v1.3/transactions/qrcode";

$data = array(
    'amount'=> 12,
    'currency'=>'USD',
    'reference'=>'123456765432',
    'ipn_url'=>'http://website.com/ipn'
);

$headers = array("Authorization"=>"Bearer 123456", "Content-Type"=>"application/json");
$options = $data;

$request = Requests::post($url, $headers, json_encode($data));

echo '<pre>';
print_r($request);

工作正常:

Requests_Response Object
(
    [body] => {"amount":"12","code_url":"https://pay.usagms.com/api/v1.0/payment/partners/XHYCRN/orders/03500201812012144392663016/retail_pay","currency":"USD","id":"201812020238444097","reference":"123456765432","time":"2018-12-02 02:44:38","timeout":120}
    [raw] => HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Length: 242
Date: Sun, 02 Dec 2018 02:44:39 GMT
Via: 1.1 google
Alt-Svc: clear
Connection: close

{"amount":"12","code_url":"https://pay.usagms.com/api/v1.0/payment/partners/XHYCRN/orders/03500201812012144392663016/retail_pay","currency":"USD","id":"201812020238444097","reference":"123456765432","time":"2018-12-02 02:44:38","timeout":120}
    [headers] => Requests_Response_Headers Object
        (
            [data:protected] => Array
                (
                    [server] => Array
                        (
                            [0] => Apache-Coyote/1.1
                        )

                    [content-length] => Array
                        (
                            [0] => 242
                        )

                    [date] => Array
                        (
                            [0] => Sun, 02 Dec 2018 02:44:39 GMT
                        )

                    [via] => Array
                        (
                            [0] => 1.1 google
                        )

                    [alt-svc] => Array
                        (
                            [0] => clear
                        )

                )

        )

    [status_code] => 200
    [protocol_version] => 1.1
    [success] => 1
    [redirects] => 0
    [url] => https://api.hantepay.com/v1.3/transactions/qrcode
    [history] => Array
        (
        )

    [cookies] => Requests_Cookie_Jar Object
        (
            [cookies:protected] => Array
                (
                )

        )

)