PHP code example of lychee / message
1. Go to this page and download the library: Download lychee/message library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lychee / message example snippets
use Lychee\Message\Text;
$xml = "<xml><ToUserName><![CDATA[username]]></ToUserName><FromUserName><![CDATA[openid]]></FromUserName><CreateTime>1529939743</CreateTime><MsgType><![CDATA[text]]></MsgType><MsgId>201806252315439427</MsgId><Content><![CDATA[Hello world]]></Content></xml>";
$msg = new Text($xml);
var_dump($msg);
// object(Lychee\Message\Text)#2 (6) {
// ["MsgType":protected]=>
// string(4) "text"
// ["ToUserName":protected]=>
// string(8) "username"
// ["FromUserName":protected]=>
// string(6) "openid"
// ["CreateTime":protected]=>
// string(10) "1529939743"
// ["MsgId":protected]=>
// string(18) "201806252315439427"
// ["properties":protected]=>
// array(1) {
// ["Content"]=>
// string(11) "Hello world"
// }
// }
use Lychee\Message\Auto;
$xml = "<xml><ToUserName><![CDATA[username]]></ToUserName><FromUserName><![CDATA[openid]]></FromUserName><CreateTime>1529939743</CreateTime><MsgType><![CDATA[text]]></MsgType><MsgId>201806252315439427</MsgId><Content><![CDATA[Hello world]]></Content></xml>";
$msg = Auto::init($xml);
var_dump(get_class($msg));
// string(19) "Lychee\Message\Text"
use Lychee\Message\Text;
$msg = new Text;
// 然后可以通过 set 开头的方法设置属性:
$msg->setToUserName("username")
->setFromUserName("openid")
->setCreateTime(time())
->setMsgId(date("YmdHis" . mt_rand(1000,9999)))
->setContent("Hello world");
var_dump($msg->toXML());
// string(248) "<xml><ToUserName><![CDATA[username]]></ToUserName><FromUserName><![CDATA[openid]]></FromUserName><CreateTime>1529939797</CreateTime><MsgType><![CDATA[text]]></MsgType><MsgId>201806252316377977</MsgId><Content><![CDATA[Hello world]]></Content></xml>"
echo $msg->ToUserName;
// username
echo $msg->Content;
// Hello world
use Lychee\Message\Text;
$msg = new Text;
$msg->setToUserName("username")
->setFromUserName("openid")
->setCreateTime(time())
->setMsgId(date("YmdHis" . mt_rand(1000,9999)))
->setContent("Hello world");
var_dump($msg->toXML());
// string(248) "<xml><ToUserName><![CDATA[username]]></ToUserName><FromUserName><![CDATA[openid]]></FromUserName><CreateTime>1529941287</CreateTime><MsgType><![CDATA[text]]></MsgType><MsgId>201806252341275858</MsgId><Content><![CDATA[Hello world]]></Content></xml>"
var_dump($msg->toArray());
// array(6) {
// ["MsgType"]=>
// string(4) "text"
// ["FromUserName"]=>
// string(6) "openid"
// ["ToUserName"]=>
// string(8) "username"
// ["CreateTime"]=>
// int(1529941287)
// ["MsgId"]=>
// int(201806252341275858)
// ["Content"]=>
// string(11) "Hello world"
// }