CURL 不发送帖子字段中的数字值 xml,字符工作正常,但数字不正常



这是在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>rn  <user>" .$username. "</user>rn  <pass>" .$password. "</pass>rn  <reference>12345</reference>rn</root>rn",
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,则这不起作用

如果用户名是爱丽丝密码mypass22和引用22,那么这将不起作用

它似乎从中剥离了数字

<root>rn  <user>" .$username. "</user>rn  <pass>" .$password. "</pass>rn  <reference>12345</reference>rn</root>rn

我有回声$username和$password,在页面上的回声中它们工作正常,所以我的猜测是,是 Curl 中的 POSTFIELDS 在剥离数字? 还是只是不发送它们?


编辑

尝试更改

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

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

但也没用

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

用法:(字符串)$username而不是$username

但是,一般来说,您需要使用正确的 php 数组作为值 CURLOPT_POSTFIELDS

喜欢这个:

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

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

最新更新