php - XML 文件没有被读取?使用 wp_remote_post
问题描述
我正在尝试使用 wordpress wp_remote_post 将 XML 文件中的数据发送到 api。但是,当我检查响应时,我收到了 0 个字节。我不确定我哪里出错了,但任何帮助将不胜感激。以下是参考文档https://help.rmscloud.com/rms-api-technical-information
<?php
$url = 'https://api.rms.com.au/rmsxml/rms_api.aspx';
$xml = 'https://spf.nz/dev/wp-content/themes/yootheme-child/rms.xml';
$args = array(
'method' => 'POST',
'timeout' => 10,
'httpversion' => '1.1',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'demoagent:aREbf2875khu*' ),
'Content-type' => 'application/xml',
'Content-length' => 83
),
'body' => array($xml),
'sslverify' => true
);
$response = wp_remote_post( $url, $args);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
echo 'Response:<pre>';
print_r( $response );
echo '</pre>';
}
?>
XML 文件:
<RMSPropertyRQ>
<Requests>
<Request>
<AgentId>1</AgentId>
<RMSClientId>3038</RMSClientId>
</Request>
<Request>
<AgentId>1</AgentId>
<RMSClientId>3038</RMSClientId>
</Request>
</Requests>
</RMSPropertyRQ>
解决方案
实际上,您不是通过 wp_remote_post 发送 XML 数据。
您已经设置'body' => array($xml)
,但 $xml 变量只是一个包含 URL 的字符串。取而代之的是,您需要获取 xml 数据并将其发送。
<?php
$url = 'https://api.rms.com.au/rmsxml/rms_api.aspx';
$xml = wp_remote_get('https://spf.nz/dev/wp-content/themes/yootheme-child/rms.xml');
$xml=$xml['body'];
///......
推荐阅读
- java - 找不到字体资源 Android
- mysql - 在 Mac 上设置 MySQL@5.7
- sql - 如何通过使用包含动态数据的列的总数来计算百分比?
- sql-server - 检查任何数据库表是否有任何行
- angular - 量角器可以找到元素,但使用 .click(); 导致找不到
- c# - 如何访问列表中对象的属性?
- batch-file - Batch script doesn't run, although its code runs in CMD
- javascript - Single array to multiple array javascript
- mysql - 为什么我不能为 MySQL 设置 root 用户?
- java - How to include _ in a numeric value in properties file?