为什么显示错误的请求而不是在邮递员中工作

时间:2020-05-06 17:21:23

标签: php curl header

我遇到了一个问题。在我的邮递员应用程序中,它可以正常工作。这是一个后方法请求。 This is my url link

Content-Type: application/x-www-form-urlencoded

Content-Type: text/plain

Cookie: ASP.NET_SessionId= urr1e3hsehli50bqkw04ot3r

{
    "request":{
        "header":{
            "username":"xxx", 
            "password":"xxx"
        },
        "body":{
            "diamond_id":"114752692",
            "shape":"Princess"
        }
    }
}

如下所示在邮递员中生成此代码

curl --location --request POST 
'https://technet.rapaport.com/HTTP/JSON/RetailFeed/GetSingleDiamond.aspx' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Content-Type: text/plain' \
--header 'Cookie: ASP.NET_SessionId=urr1e3hsehli50bqkw04ot3r' \
--data-raw '{
    "request":{
        "header":{
            "username":"xxx", 
            "password":"xxx"
        },
        "body":{
            "diamond_id":"114752692",
            "shape":"Princess"
        }
    }
}'

但是我的php代码中存在问题。我这样转换此代码在php及以下。

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://technet.rapaport.com/HTTP/JSON/RetailFeed/GetSingleDiamond.aspx",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS =>"{\r\n\"request\": {\r\n\"header\":{\r\n\"username\":\"xxx\",\r\n \"password\": \"xxx\"\r\n},\r\n\"body\": {\r\n\"diamond_id\":\"114752692\", \r\n\"shape\":\"Princess\"\r\n}\r\n}\r\n}",
    CURLOPT_HTTPHEADER => array(
        "Content-Type: application/x-www-form-urlencoded",
        "Content-Type: application/json",
        "Cookie: ASP.NET_SessionId=urr1e3hsehli50bqkw04ot3r"
    ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

在我的本地主机中,当我浏览http://localhost/price/index.php时,会向我显示错误。

错误的请求-无效的标题

HTTP错误400。请求具有无效的标头名称。我不知道为什么它会向我显示错误。

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,只需替换

"Content-Type: application/x-www-form-urlencoded",
"Content-Type: application/json",
"Cookie: ASP.NET_SessionId=urr1e3hsehli50bqkw04ot3r"

收件人

'Content-Type' => 'application/x-www-form-urlencoded',
'Content-Type' => 'text/plain',
'Cookie'       => 'ASP.NET_SessionId=wvxova2bwht020vw4begtpiw'

现在对我来说很好