微站制作教程示意圖
  • 更新時(shí)間:2024-10-28 06:29:17
  • 網(wǎng)站建設(shè)
  • 發(fā)布時(shí)間:1年前
  • 229

推來客網(wǎng)站制作公司最近發(fā)現(xiàn)很多人都在問什么是微站,微站有什么用,微站怎么做,微站的搭建流程。對喜歡學(xué)習(xí)的人有幫助。

第一步:首先,您需要在微信公眾平臺(tái)注冊自己的服務(wù)號(hào)或訂閱號(hào)。

微網(wǎng)站制作教程圖解1:注冊自己的服務(wù)號(hào)或者訂閱號(hào)

第二步:公眾號(hào)申請成功后,即可使用其基本功能。當(dāng)然,如果你要做自己的微信官網(wǎng),還需要進(jìn)入開發(fā)模式,接入第三方接口。

微網(wǎng)站制作教程圖解2 微網(wǎng)站制作圖解3 微官網(wǎng)

第三步:注冊成功并登錄第三方界面,將注冊的微信公眾號(hào)添加到第三方界面,所需信息在微信公眾號(hào)設(shè)置下的賬號(hào)信息中。

注冊成功并登陸第三方接口

第四步:添加公眾號(hào)后,連接公眾平臺(tái)和第三方接口,如圖:登錄微信公眾平臺(tái),點(diǎn)擊左下方【開發(fā)者中心】,點(diǎn)擊開發(fā)模式。

登錄微信公眾平臺(tái)

第五步:最后,設(shè)計(jì)自己的微信官網(wǎng)。并且可以在線預(yù)覽。

設(shè)計(jì)自己的微信官網(wǎng),并且可在線預(yù)覽

完成以上步驟并驗(yàn)證訂閱號(hào)或服務(wù)號(hào)后,就可以進(jìn)行微信的二次開發(fā)了。比如我要?jiǎng)?chuàng)建一個(gè)帶群發(fā)功能的接口,需要用到微信接口:

1.獲取訪問令牌

2.增加臨時(shí)素材界面

3.上傳圖文消息素材界面

4.調(diào)用群發(fā)接口

接口代碼示例:WeixinApi.class.php

?PHP

WeixinApi 類{

私有$appid, $appsecret, $media_id;

公共函數(shù)__construct($appid='',$appsecret=''){

$this-appid=$appid;

$this-appsecret=$appsecret;

}

//獲取令牌

公共函數(shù)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_credentialappid={$this-appid}secret={$this-appsecret}');

$tokenarr=json_decode($token,true);

setcookie('exptime', $tokenarr['expires_in'], 0, '/');

setcookie('access_token', $tokenarr['access_token'], 0, '/');

setcookie('create_time',time(),0,'/');

}別的{

$tokenarr=數(shù)組(

'access_tokenqu

ot;=>$_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數(shù)據(jù)提交
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: 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;
}

//群發(fā)接口
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($appid,$appsecret);
$result=$weixin->upload("image","圖片路徑")->uploadnews("文章標(biāo)題","文章內(nèi)容")->sendall();

我們專注高端建站,小程序開發(fā)、軟件系統(tǒng)定制開發(fā)、BUG修復(fù)、物聯(lián)網(wǎng)開發(fā)、各類API接口對接開發(fā)等。十余年開發(fā)經(jīng)驗(yàn),每一個(gè)項(xiàng)目承諾做到滿意為止,多一次對比,一定讓您多一份收獲!

本文章出于推來客官網(wǎng),轉(zhuǎn)載請表明原文地址:https://www.tlkjt.com/web/11978.html
推薦文章

在線客服

掃碼聯(lián)系客服

3985758

回到頂部