【问题标题】:CURL not sending number values in post fields xml, Characters work fine, but not numbersCURL 不在 post 字段 xml 中发送数字值,字符工作正常,但不是数字
【发布时间】:2019-06-13 10:53:12
【问题描述】:

这是在 wordpress 上。

如果 用户名、密码和参考都以字母开头,然后请求和响应工作正常。

但如果用户名、密码或参考是一个数字,那么它似乎不会发送。

在邮递员上它工作正常。

我试着把它当作一个变量

$data = "body xml"
 CURLOPT_POSTFIELDS => $data,

这不起作用,但我也不确定如何在其中获得 $user 和 $pass。

$username = $current_user->first_name;
$password = $current_user->last_name;


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_PORT => "xxxxxxxxxxxx",
  CURLOPT_URL => "xxxxxxxxxxxxxxxxx",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "<root>\r\n  <user>" .$username. "</user>\r\n  <pass>" .$password. "</pass>\r\n  <reference>12345</reference>\r\n</root>\r\n",

  CURLOPT_HTTPHEADER => array(
    "Accept: application/xml",
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Content-Type: application/xml",
    "Host: xxxxxxxxxxxx",
    "Postman-Token: xxxxxxxxxxxxxxxx",
    "User-Agent: PostmanRuntime/7.13.0",
    "accept-encoding: gzip, deflate",
    "cache-control: no-cache",
    "content-length: 107"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "There are no orders to show on this account ";
} else {

结果显示在表格等中

所以如果用户名是 Bob,密码是 mypass,引用是 ABC123,那么它会正常工作

但是

如果用户名是 Sid,密码 1234 并引用 ABC444,那么这将不起作用

如果用户名是 Alice 密码 mypass22 并且引用 22 那么这将不起作用

似乎去掉了 This 中的数字

<root>\r\n  <user>" .$username. "</user>\r\n  <pass>" .$password. "</pass>\r\n  <reference>12345</reference>\r\n</root>\r\n

我已经回显了 $username 和 $password 并且在页面上的回显中它们工作正常,所以我的猜测是,它是 Curl 中的 POSTFIELDS 剥离数字?或者只是不发送?


编辑

尝试改变

CURLOPT_POSTFIELDS => "<root>\r\n  <user>" .$username. "</user>\r\n  <pass>" .$password. "</pass>\r\n  <reference>12345</reference>\r\n</root>\r\n",

 $input_xml = '<root>
      <user>sid</user>
      <pass>Mypass</pass>
      <reference>12345</reference>
    </root>';

 CURLOPT_POSTFIELDS => $input_xml,

但也没有用

【问题讨论】:

    标签: wordpress curl numbers httprequest


    【解决方案1】:

    不知道为什么会发生这种情况(应该进行调试),但您可以轻松地对这些变量进行字符串化 - 这对数字和字符串都有效。

    用法:(字符串)$username 代替 $username

    但是,通常情况下,您需要使用正确的 php 数组作为 CURLOPT_POSTFIELDS 的值

    像这样:

    CURLOPT_POSTFIELDS => array(
    'user'=>$username, 
    'pass'=>$password,
    'reference'=>$reference
    ),
    

    通过这种方式,接收方将直接获得类似 $_POST["user"] 的数据,而不是任何应该解析的 XML。

    【讨论】:

    • 您好,感谢您的回复。您能否给我一个清楚的例子,说明(字符串)的含义以及如何使用它
    • 只需在给定代码中将 $username 替换为 (string)$username 即可。这里:CURLOPT_POSTFIELDS => "\r\n " .$username。 "\r\n " .$password。 "\r\n 12345\r\n\r\n",
    • \r\n " .(string)$username."\r\n " .(string)$password."\r\n 12345\r\n\r\n 我这样做了,但还是不行。
    • 如果也没有帮助,请尝试使用正确的发布数据。我正在为我的答案添加正确的数据
    • 不幸的是,请求发送到的地方只接受 XML。我真的被困在这上面了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    相关资源
    最近更新 更多