[英]post request works in postman but not in php
這些是 postman 中的參數:
發布 URL:
https://rest-api.wm.com/user/authenticate
標題:
apikey: 6277FF29BB19666078AC
content-type: application/json
正文:(下一個示例中的用戶名和密碼不正確)
{"username":"xxx@xxx.com","password":"xxx","locale":"en_US"}
結果,我收到了一個 JSON 頁面,確認我已登錄該網站。
但是當我將此代碼轉換為 PHP
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://rest-api.wm.com/user/authenticate",
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 =>"{\"username\":\"xxx@xxx.com\",\"password\":\"xxx\",\"locale\":\"en_US\"}",
CURLOPT_HTTPHEADER => array(
"apikey: 6277FF29BB19666078AC",
"content-type: application/json",
"Content-Type: text/plain"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
當我啟動腳本時,這不起作用我收到此錯誤:
{"status":"failed","errorMsg":"Please enter valid input","statusCode":400}
很奇怪這個 cna 如何在 postman 而不是在 PHP 中工作,有什么幫助嗎?
嘗試將“Content-Length”添加到 CURLOPT_HTTPHEADER:
CURLOPT_HTTPHEADER => array(
"apikey: 6277FF29BB19666078AC",
"Content-Type: application/json",
"Content-Length: " . strlen($json))
),
您需要 json 的字符串長度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.