相信對於大多數的微信公衆號開發的初學者來說,由於微信提供的文檔過於簡潔,所以這無疑是對我們的巨大考驗。
但是,苦心人,天不負。在強烈的“我能行”這一自我暗示下,經過在各大網站上的查詢、電子書籍類的讀閱,經過無數次的嘗試,終於能夠實現一次事件同時回覆用戶多條信息的功能了。額,不說廢話了,下面展示我的最終成果。
(公衆號是接管過來自己開發的)
首先,創建公衆號子菜單的點擊事件
const APPID = '你的微信appid';
const SECRET = '你的secret';
const ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=';
const GET_MENU_URL = 'https://api.weixin.qq.com/cgi-bin/menu/get?access_token=';
const CREATE_MENU_URL = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=';
/**
* createmenu
* 創建菜單
* @access public
* @param void
* @since 1.0
* @return array
*/
public function actioncreatemenu()
{
$access_token_url = self::ACCESS_TOKEN_URL.self::APPID.'&secret='.self::SECRET;
$wdata =json_decode($this->curl_get($access_token_url),true);
$create_menu_url = self::CREATE_MENU_URL.$wdata['access_token'];
$menu = json_encode($this->getMenuConfig(),JSON_UNESCAPED_UNICODE);
$data = $this->curl_post($create_menu_url,$menu);
echo $data;exit;
}
private function getMenuConfig()
{
$data = array();
$data = Array(
'button' => Array(
Array(
'name' => '你關心的',
'sub_button' => Array(
Array(
'type' => 'click',
'name' => '邀友有禮',
'key' => 'V1001_INVITE_GIFT'
)
)
)
)
}
public function curl_post($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $output;
}
然後在網頁運行 createmenu方法,微信公衆號菜單就創建好了。
這些都是前期的準備。
下面是點擊事件的處理
$this->sendIMG($media_id , $open_id , $token);
這行代碼中的 media_id ,是通過微信接口上傳媒體素材返回的微信獨有的標識,上傳素材的代碼在最後面展示
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$event = $postObj->Event;
$time = $postObj->CreateTime;
$open_id = trim($fromUsername);
$MsgType = $postObj->MsgType;
file_put_contents('/tmp/test.log', $MsgType, FILE_APPEND);
switch ($event) {
case 'CLICK':
$token = $this->token();
$this->sendText($open_id , $token);
$resultIMG = '圖片相對於服務器下的路徑';
$media_id = $this->add_m($resultIMG);
$this->sendIMG($media_id , $open_id , $token);
/*
$returnTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>";
$resultStr = sprintf($returnTpl,$fromUsername , $toUsername , time(), 'image' ,$media_id);
echo $resultStr; */
break;
}
function sendText($open_id , $token) {
$data = '{ "touser" : "'.$open_id.'",
"msgtype" : "text",
"text" : {
"content" : "敬請期待"
}
}';
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token;
$result = $this->https_request($url , $data);
var_dump($result);
}
function sendIMG($media_id , $open_id , $token) {
$data = '{ "touser" : "'.$open_id.'",
"msgtype" : "image",
"image" : {
"media_id" : "'.$media_id.'"
}
}';
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token;
$result = $this->https_request($url , $data);
var_dump($result);
}
上傳媒體素材,這裏上傳的是圖片,所以傳的參數則是一張圖片的路徑
function add_m($img) {
$path = '這裏填寫圖片上傳的相對路徑'.$img ;
//$img = 'web/image/qrcode/20161229/10.jpg';
if (class_exists ( '\CURLFile' )) {//關鍵是判斷curlfile,官網推薦php5.5或更高的版本使用curlfile來實例文件
$filedata = array (
'fieldname' => new \CURLFile ( realpath ( $path ), 'image/jpeg' )
);
} else {
$filedata = array (
'fieldname' => '@' . realpath ( $path )
);
}
$url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->token()."&type=image";
$result = $this->https_request($url , $filedata);
$data = json_decode($result);
return $data->media_id;
}
(通過 path ,必須能夠在服務器下訪問到圖片)最後,說明一下,我的圖片是能都通過網絡訪問到的,比如(http://www.image.****.com)
但是,當我將這個圖片的地址添加到上傳媒體素材進行上傳的時候,結果是上傳失敗,也就沒有了本來應該返回的 media_id,所以採用了獲取路徑上傳的方法
在這裏如果有更好的方法,歡迎留言艾特我,大家相互交流,共同進步!!!