联系我们:416326801#qq.com(#改成@)

Latest from the blog

构建微信公众号被动回复image图片消息xml

标签: XML Weixin

public static function transmitImage($message) {
    if( !is_array($message) || empty($message) ) {
        return;
    }
    $xmlTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId>%s</MediaId></Image></xml>";
    $xml = sprintf($xmlTpl, $message['ToUserName'], $message['FromUserName'], $message['CreateTime'], $message['Image']['MediaId']);
    return $xml;
}

构建微信公众号被动回复news多图文消息xml

标签: XML Weixin


 public static function transmitNews($message) {
    if( !is_array($message) || empty($message) ) {
        return;
    }
    $itemTpl = "<item><Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description><PicUrl><![CDATA[%s]]></PicUrl><Url><![CDATA[%s]]></Url></item>";
    $item_str = "";
    foreach ( $message['Articles'] as $item ) {
        $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
    }
    $xmlTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>%s</ArticleCount><Articles>$item_str</Articles></xml>";
    $xml = sprintf($xmlTpl, $message['ToUserName'], $message['FromUserName'], $message['CreateTime'], $message['ArticleCount']);
    return $xml;
}

PHP将数组转换成XML

标签: PHP XML

//将array转为XML
public static function arrayToXml($array) {
    if( !is_array($array) || empty($array) ) {
        return false;
    }
    $xml = "<xml>";
    foreach ( $array as $key => $val ) {
        if( is_numeric($val) ) {
            $xml.="<".$key.">".$val."</".$key.">";
        } else {
           $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
        }
    }
    $xml.="</xml>";
    return $xml;
}

PHP将XML转换成数组

标签: PHP XML

简单的XML
public static function xmlToArray($xml) {
    libxml_disable_entity_loader(true);
    $result = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    if( is_array($result) ) {
        return $result;
    }
    return array();
}
带名字空间的XML
//xml转array
public static function xmlToArray($xml) {
	if( !$xml || empty($xml) ){
		return array();	
	}
	$dom = new DOMDocument();
	$dom->loadXML($xml);
	return self::getArray($dom->documentElement);
}
	
private static function getArray($node) {
	$array = false;
	if ($node->hasAttributes()) {
		foreach ($node->attributes as $attr) {
			$array[$attr->nodeName] = $attr->nodeValue;
		}
	}
	
	if ($node->hasChildNodes()) {
		if ($node->childNodes->length == 1) {
			$array[$node->firstChild->nodeName] = self::getArray($node->firstChild);
		} else {
			foreach ($node->childNodes as $childNode) {
				if ($childNode->nodeType != XML_TEXT_NODE) {
					$array[$childNode->nodeName][] = self::getArray($childNode);
				}
			}
		}
	} else {
		return $node->nodeValue;
	}
	return $array;
}