dongying7847 2019-07-14 09:36
浏览 92

如何通过POST读取第三方在URL上发送的XML消息?

A 3rd party APP delivers a XML message at a URL. I want to be able to read and operate with that message at WordPress.

I believe that SimpleXMLElement is the key, rather than creating a class. But many errors are returned based on what I put as a parameter.

add_action( 'rest_api_init', function () {
  register_rest_route( 'webhook', '/v1', array(
    'methods'  => 'POST',
    'callback' => 'myfunction',
  ));
});

function myfunction () {

    $mymessage = new SimpleXMLElement($xmlstr);

    $messageType = $mymessage->cgCallback[0]->existingField;

    echo $messageType ;
}

$mymessage = new SimpleXMLElement($xmlstr); -> Returns "Undefined variable: xmlstr"

$mymessage = new SimpleXMLElement($data); -> Returns "Uncaught TypeError: SimpleXMLElement::__construct() expects parameter 1 to be string"

$mymessage = new SimpleXMLElement($message); -> Returns "Uncaught Exception: String could not be parsed as XML"

  • 写回答

1条回答 默认 最新

  • dongpinyao2203 2019-07-15 13:46
    关注

    Ok, got the solution: This will not work with SimpleXMLElement but with simplexml_load_string and requires file_get_contents('php://input'):

    function myfunction () {
    
        $postData = file_get_contents('php://input');
        $xml = simplexml_load_string($postData);
    
        $messageType = $xml->header[0]->existingField;
    
        echo $messageType ;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办