发布请求在邮递员中有效,但在php中不起作用

问题描述 投票:0回答:1

这些是邮递员中的参数:

POST URL:

https://rest-api.wm.com/user/authenticate

标题:

apikey: 6277FF29BB19666078AC
content-type: application/json

正文:(在下一个示例中,用户名和密码不正确)

{“用户名”:“ [email protected]”,“密码”:“ xxx”,“区域设置”:“ 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\":\"[email protected]\",\"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在邮递员中而不是在PHP中如何工作,有什么帮助吗?

php curl post http-post postman
1个回答
0
投票

尝试将“ Content-Length”添加到CURLOPT_HTTPHEADER:

CURLOPT_HTTPHEADER => array(
      "apikey:  6277FF29BB19666078AC",
      "Content-Type: application/json",
      "Content-Length: " . strlen($json))
 ),

您需要json的字符串长度。

© www.soinside.com 2019 - 2024. All rights reserved.