利用官方接口实现内容的同步的几个步骤:
1.获取token接口,appid和secret可以从微信公众号后台查看。
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret";
2.上传封面图片,获取返回的id
https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$token
3.构造上传的内容
4.发送构造的内容
https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=$token
完整代码,在functions.php中添加即可。
//获取微信公众号的token
function GetWeixinToken(){
//$headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret";
$curl=curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$output =curl_exec($curl);
//var_dump($output);
$result=json_decode($output);
curl_close($curl);
//var_dump($result->access_token);
// var_dump($url);
return $result->access_token;
}
//构造文章信息
// {
// "articles": [{
// "title": TITLE,
// "thumb_media_id": THUMB_MEDIA_ID,
// "author": AUTHOR,
// "digest": DIGEST,
// "show_cover_pic": SHOW_COVER_PIC(0 / 1),
// "content": CONTENT,
// "content_source_url": CONTENT_SOURCE_URL,
// "need_open_comment":1,
// "only_fans_can_comment":1
// }]
// }
function GetArticalInfo($token){
global $post;
$postid=$post->ID;
$output=get_post($postid);
//var_dump($output);
//标题
$title=$output->post_title;
//缩略图
$large_image_url = wp_get_attachment_image_src(get_post_thumbnail_id($postid),"medium");
//var_dump($large_image_url[0]);
if($large_image_url[0]!="")
{
//$file="/var/www/html/wordpress/wp-content/uploads/2020/11/2020-11-24-202040.png";
$file=str_replace("https://www.rdonly.com","/var/www/html/wordpress",$large_image_url[0]);
//var_dump($file);
}
if($file==""){
return ;
}
$thumb_media_id=UploadImageToWeixin($token,$file);
//作者
$author="Appfuns";
$digest="";
$show_cover_pic=0;
//内容
// 上传文章中的所有图片
$newcontent=GetAllImages($token,$output->post_content);
//加上网站链接地址
$link="";
$custom_fields = get_post_custom($postid);
$count=0;
foreach($custom_fields as $keys=>$values){
if ( '_' == $keys{0} )
continue;
else{
$count++;
}
}
if($count){
foreach($custom_fields as $keys=>$values){
if ( '_' == $keys{0} )
continue;
foreach($values as $key => $value){
$link=$link."".$value."
";
}
}
}
//var_dump($link);
$content=$newcontent.$link;
//原文地址
$content_source_url=$output->guid;
$need_open_comment=0;
$only_fans_can_comment=0;
$arrcontent=array();
$arrcontent["title"]=$title;
$arrcontent["thumb_media_id"]=$thumb_media_id;
$arrcontent["author"]=$author;
$arrcontent["digest"]=$digest;
$arrcontent["show_cover_pic"]=$show_cover_pic;
$arrcontent["content"]=$content;
$arrcontent["content_source_url"]=$content_source_url;
$arrcontent["need_open_comment"]=$need_open_comment;
$arrcontent["only_fans_can_comment"]=$only_fans_can_comment;
$data=array("articles"=>array($arrcontent));
// var_dump($data);
return json_encode($data,JSON_UNESCAPED_UNICODE);
}
//获取文章中的所有图片
function GetAllImages($token,$content){
preg_match_all('//i', $content, $matches);
//var_dump($matches);
for($i=0;$i new CURLFile($file)
);
$curl=curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $filedata);
$output =curl_exec($curl);
// var_dump($output);
$result=json_decode($output);
curl_close($curl);
var_dump($result);
return $result->url;
}else
{
return "";
}
// var_dump($url);
// return $result->media_id;
}
// 上传图片
function UploadImageToWeixin($token,$file){
//$token=GetWeixinToken();
//$file="/var/www/html/wordpress/wp-content/uploads/2020/11/2020-11-24-202040.png";
$url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$token."&type=thumb";
$filedata = array(
'media'=> new CURLFile($file)
);
$curl=curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $filedata);
$output =curl_exec($curl);
// var_dump($output);
$result=json_decode($output);
curl_close($curl);
//var_dump($result->media_id);
// var_dump($url);
return $result->media_id;
}
//调用接口发送数据
function SetWeixinArtical(){
$token=GetWeixinToken();
$data=GetArticalInfo($token);
// var_dump($data);
// $headers = array("Content-type:text/json;charset=utf-8");
$url="https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=".$token;
// var_dump($url);
$curl=curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $data);
$output =curl_exec($curl);
// //var_dump($output);
$result=json_decode($output);
curl_close($curl);
// //var_dump($result);
// // var_dump($url);
// return $result->access_token;
}
add_action('publish_post','SetWeixinArtical');