首页 > 解决方案 > 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>

标签: phpxmlwordpressapirms

解决方案


实际上,您不是通过 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'];
///......

推荐阅读