微站制作教程示意图
  • 更新时间:2024-05-14 07:21:18
  • 网站建设
  • 发布时间:11个月前
  • 263

推来客网站制作公司最近发现很多人都在问什么是微站,微站有什么用,微站怎么做,微站的搭建流程。对喜欢学习的人有帮助。

第一步:首先,您需要在微信公众平台注册自己的服务号或订阅号。

微网站制作教程图解1:注册自己的服务号或者订阅号

第二步:公众号申请成功后,即可使用其基本功能。当然,如果你要做自己的微信官网,还需要进入开发模式,接入第三方接口。

微网站制作教程图解2 微网站制作图解3 微官网

第三步:注册成功并登录第三方界面,将注册的微信公众号添加到第三方界面,所需信息在微信公众号设置下的账号信息中。

注册成功并登陆第三方接口

第四步:添加公众号后,连接公众平台和第三方接口,如图:登录微信公众平台,点击左下方【开发者中心】,点击开发模式。

登录微信公众平台

第五步:最后,设计自己的微信官网。并且可以在线预览。

设计自己的微信官网,并且可在线预览

完成以上步骤并验证订阅号或服务号后,就可以进行微信的二次开发了。比如我要创建一个带群发功能的接口,需要用到微信接口:

1.获取访问令牌

2.增加临时素材界面

3.上传图文消息素材界面

4.调用群发接口

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

?PHP

WeixinApi 类{

私有$appid, $appsecret, $media_id;

公共函数__construct($appid='',$appsecret=''){

$this-appid=$appid;

$this-appsecret=$appsecret;

}

//获取令牌

公共函数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=数组(

'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数据提交
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;
}

//群发接口
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("文章标题","文章内容")->sendall();

我们专注高端建站,小程序开发、软件系统定制开发、BUG修复、物联网开发、各类API接口对接开发等。十余年开发经验,每一个项目承诺做到满意为止,多一次对比,一定让您多一份收获!

本文章出于推来客官网,转载请表明原文地址:https://www.tlkjt.com/web/11978.html

在线客服

扫码联系客服

3985758

回到顶部