请登录后探索更多精彩内容!
修改dede/templets/article_add.htm在编辑器上面,代码<<?php PrintAutoFieldsAdd($cInfos['fieldset'],'autofield'); ?>>上面新增采集代码:
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">文章采集</label>
<div class="layui-input-inline" style="width:800px;">
<input name="wx_url" id="wx_url" type="text" class="layui-input" placeholder="粘贴公众号文章URL链接">
</div>
<div class="layui-input-inline" style="width:80PX;">
<input type="button" class="layui-btn layui-btn-normal" value="采集文章" onClick="weixincaiji();" >
样式根据自己的样式新增,主代码:<input name="wx_url" id="wx_url" type="text" class="layui-input" placeholder="粘贴公众号文章URL链接">和<input type="button" class="layui-btn layui-btn-normal" value="采集文章" onClick="weixincaiji();" >在此代码下面新增JS代码:<script>
//微信采集
function weixincaiji(){
var wxurl = $("#wx_url").val();
//alert(wxurl);
$.ajax({
url: "wxcj.php",
type: "POST",
data: {
wx_url: wxurl
},
dataType: 'json',
success: function(result) {
// 在这里处理服务器返回的数据
$("#title").val(result.title);
$("#picname").val(result.img);
editor1.html(result.body);
$("textarea[name='body']").val(result.body);
error: function(xhr, status, error) {
// 处理请求失败的情况
$("#yuanwentititletext").html('ajax请求失败');
}
});
</script>
其中:wxcj.php为后台dede根目录文件,post提交参数wx_url也就是公众号文章URL链接,返回格式json ,返回数据有3个,标题、首图和内容,最后在 success: function里面把返回的结果直接赋值在相应的位置。然后是wxcj.php文件源码:header('Content-type: application/json');
if (isset($_POST['wx_url']) && trim($_POST['wx_url']) !== '') {
$url = $_POST['wx_url'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT,10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
$data = curl_exec($curl);
curl_close($curl);
//print_r($data);
$dom = new DOMDocument();
libxml_use_internal_errors(true); // 忽略HTML解析错误
$dom->loadHTML($data);
libxml_clear_errors();
// 使用DOMXPath来查询节点
$xpath = new DOMXPath($dom);
// 提取标题
$msgTitleNode = $xpath->query('//h1[@id="activity-name"]');
$msgTitle = $msgTitleNode->length > 0 ? trim($msgTitleNode->item(0)->nodeValue) : '';
// 提取内容
$msgContentNodes = $xpath->query('//div[contains(@class, "rich_media_content")]');
$msgContent = $msgContentNodes->length > 0 ? $dom->saveHTML($msgContentNodes->item(0)) : '';
// 提取图像URL
$msgImageNode = $xpath->query('//meta[@property="og:image"]');
$msgImage = $msgImageNode->length > 0 ? $msgImageNode->item(0)->getAttribute('content') : '';
//处理内容,处理DIV style="visibility: hidden; opacity: 0; "防止msgContent内容放在编辑器里面变成空白
$msgContent = str_replace("visibility: hidden; opacity: 0;","",$msgContent);
//处理图片
$msgContent = str_replace("data-src","src",$msgContent);
$msgContent = str_replace("https://mmbiz","https://i0.wp.com/mmbiz",$msgContent);
$msgImage = str_replace("https://mmbiz","https://i0.wp.com/mmbiz",$msgImage);
//返回结果
echo json_encode(array('title' => $msgTitle,'img' => $msgImage, 'body' => $msgContent));exit;
暂无评论
请先登录后发表评论!
暂无评论