近發現特別多的人在問微網站是什麼,微網站有什麼用,微網站怎麼做,微網站建設流程等問題,今天,在這裡為大家統一解答,希望能對各位愛學習人士有所幫助。

步:先,需要在 WordPress 微信公眾平臺上註冊一個自己的服務號或者訂閱號。

第二步:申請公眾賬號成功後,便可以使用其基本功能了,當然,要想做成自己的 WordPress 微信官網,則需要進入開發模式,接入第三方介面。

第三步:註冊成功並登陸第三方介面,將註冊好的 WordPress 微信公眾號新增到第三方介面上,所需資訊在 WordPress 微信公眾號設定下的賬號資訊裡。

第四步:新增公眾賬號後,連線公眾平臺與第三方介面,如圖:登入 WordPress 微信公眾平臺,點選左側下方的【開發者中心】,點選開發模式。

第五步:後,設計自己的 WordPress 微信官網。並且可線上預覽。

完成以上步驟後,並且認證訂閱號或服務號後就可以做 WordPress 微信的二次開發了,比如我要製作一個群發功能的介面,需要使用一下 WordPress 微信介面:

1 、獲取 access token

2 、新增臨時素材介面

3 、上傳圖文資訊素材介面

4 、呼叫群發介面

介面程式碼示例:WeixinApi.class.php

WordPress APPid=$WordPress APPid;
$this->WordPress APPsecret=$WordPress APPsecret;
}

//獲取 token
public function getToken(){
if($_COOKIE[“exptime”]==”” || time()-$_COOKIE[“create_time”]>=$_COOKIE[“exptime”]){
$token=@file_get_contents(“https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&WordPress APPid={$this->WordPress APPid}&secret={$this->WordPress APPsecret}”);
$tokenarr=json_decode($token,true);
setcookie(“exptime”,$tokenarr[“expires_in”],0,’/’);
setcookie(“access_token”,$tokenarr[“access_token”],0,’/’);
setcookie(“create_time”,time(),0,’/’);
}else{
$tokenarr=array(
“access_token”=>$_COOKIE[“access_token”],
“expires_in”=>$_COOKIE[“exptime”],
“create_time”=>$_COOKIE[“create_time”]
);
}
return $tokenarr;
}

private function sockupload($phost,$pport,$purl,$filename,$file_data=array()){
$host = $phost;
$port = $pport;
$errno = ”;
$errstr = ”;
$timeout = 30;
$url = $purl;

/*$form_data = array(
‘name’ => ‘lijie’,
‘gender’ => ‘man’,
);*/

$file_data = array(
array(
‘name’ => ‘media’,
‘filename’ => $filename,
//’path’ =>’1.jpg’
)
);

// create connect
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);

if(!$fp){
return false;
}

// send request
srand((double)microtime()*1000000);
$boundary = “—————————“.substr(md5(rand(0,32000)),0,10);

$data = “–$boundaryrn”;

// form data
if(count($form_data)>0){
foreach($form_data as $key=>$val){
$data .= “Content-Disposition: form-data; name=””.$key.””rn”;
$data .= “Content-type:text/plainrnrn”;
$data .= rawurlencode($val).”rn”;
$data .= “–$boundaryrn”;
}
}

// file data
if($filename!=””){
foreach($file_data as $file){
$data .= “Content-Disposition: form-data; name=””.$file[‘name’].””; filename=””.$file[‘filename’].””rn”;
$pinfo=pathinfo($file[‘filename’]);
$data .= “Content-Type: “.$pinfo[“extension”].”rnrn”;
$data .= implode(“”,file($file[‘filename’])).”rn”;
$data .= “–$boundaryrn”;
}
}

$data .=”–rnrn”;

$out = “POST ${url} HTTP/1.1rn”;
$out .= “Host:${host}rn”;
$out .= “Content-type:multipart/form-data; boundary=$boundaryrn”; // multipart/form-data
$out .= “Content-length:”.strlen($data).”rn”;
$out .= “Connection:closernrn”;
$out .= “${data}”;

fputs($fp, $out);

// get response
$response = ”;
while($row=fread($fp, 4096)){
$response .= $row;
}

$pos = strpos($response, “rnrn”);
$response = substr($response, $pos+4);

return $response;
}

//json 資料提交
private function jsonUpload($url,$jsdata){
$data_string = $jsdata;
$ch = curl_init();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: WordPress APPlication/json;charset=utf-8’,
‘Content-Length: ‘ . strlen($data_string))
);

$result = curl_exec($ch);
curl_close ( $ch );
return $result;
}

//上傳媒體介面
public function upload($type,$filename){
$tokens=$this->getToken();
$result=$this->sockupload(“api.weixin.qq.com”,80,”/cgi-bin/media/upload?access_token=”.$tokens[“access_token”].”&type={$type}”,$filename);
$media=json_decode($result,true);
$this->media_id=$media[‘media_id’];
return $this;
}

//上傳素材
public function uploadnews($title,$content,$author=””,$url=””,$digest=””,$show_cover_pic=”1″){
$articles=array(
“articles”=>array(
array(
“thumb_media_id”=>$this->media_id,
“author”=>$author,
“title”=>urlencode($title),
“content_source_url”=>$url,
“content”=>urlencode($content),
“digest”=>$digest,
“show_cover_pic”=>”1”
)
)
);

$tokens=$this->getToken();
$news=urldecode(json_encode($articles));
$result=$this->jsonUpload(“api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=”.$tokens[“access_token”].””,$news);
$newsdata=json_decode($result,true);
$this->media_id=$newsdata[“media_id”];
return $this;
}

//群發介面
public function sendall(){
$arr=array(
“filter”=>array(“is_to_all”=>true,”group_id”=>””),
“mpnews”=>array(“media_id”=>$this->media_id),
“msgtype”=>”mpnews”
);
$json=json_encode($arr);
$tokens=$this->getToken();
$result=$this->jsonUpload(“api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=”.$tokens[“access_token”].””,$json);
return $result;
}

}

使用:

$weixin=new WeixinApi($WordPress APPid,$WordPress APPsecret);
$result=$weixin->upload(“image”,” 圖片路徑”)->uploadnews(“ 文章標題”,” 文章內容”)->sendall();