function buildXML(&$doc, $data, $append = null) { foreach ($data as $k => $v) { $app = $append ? $append : $doc; if (is_array($v) || is_object($v)) { $c = $app->appendChild($doc->createElement($k)); buildXML($doc, $v, $c); } else { $c = $app->appendChild($doc->createElement($k, (string) $v)); } } }
$data['articles'] = array(); if ($mysql->num_row > 0) { $transaction['media_id'] = $config['subscribe_reply_content']; foreach ($mysql->data as $key => $item) { $transaction['content'] .= $item['title'] . "\n"; $data['articles'][$key] = array('title' => $item['title'], 'digest' => $item['digest'], 'thumb' => $item['thumb_media_id'], 'url' => $item['url']); } } break; case $wechat::MSGTYPE_IMAGE: $transaction['media_id'] = $data['media_id'] = $config['subscribe_reply_content']; $thumb = $SYSCONFIG['SERVER']['baseUrl'] . "api/mediaSource.php?media_id=%s®=%s"; $transaction['content'] = sprintf($thumb, $config['subscribe_reply_content'], $schema); break; } $responseXML = buildXML($postArr['FromUserName'], $postArr['ToUserName'], $data); $transaction['raw_data'] = $wechat->raw_json_encode($data); log_transaction($transaction); } } catch (Exception $ex) { # log the exception log_error('snippet: subscribe_reply', json_encode($postArr), $ex->getMessage()); } // encrypted message $responseStr = ''; if ($encrypt_type == 'aes') { $encrypt_array['responseXML'] = $responseXML; $result = $wechat->encryptMessage($encrypt_array); if ($result['hasError']) { die; }
<?php header('Content-Type: text/xml'); $doc = new DOMDocument('1.0'); $doc->formatOuput = true; buildXML($doc, array('response' => $items)); echo $doc->saveXML();