本文目錄一覽:
使用php如何獲取微信文章?
可以嘗試使用DOM操作庫simple-html-dom.php,快速獲取HTML結構的內容:
?php
require dirname(__FILE__).’/simple_html_dom.php’;
$html = file_get_html(”);
$articles = array();
foreach($html-find(‘article.newsentry’) as $article) {
$item[‘time’] = trim($article-find(‘time’, 0)-plaintext);
$item[‘title’] = trim($article-find(‘h2.newstitle’, 0)-plaintext);
$item[‘content’] = trim($article-find(‘div.newscontent’, 0)-plaintext);
$articles[] = $item;
}
print_r($articles);
可以把抓取到的內容寫入置於內存上的SQLite(/run/shm/php/crawler.db3),避免頻繁的磁碟IO.
PHP如何獲得微信公眾平台關注用戶的基本信息
1、引導用戶進入授權頁面同意授權,獲取code
2、通過code換取網頁授權access_token(與基礎支持中的access_token不同)
3、如果需要,開發者可以刷新網頁授權access_token,避免過期
4、通過網頁授權access_token和openid獲取用戶基本信息(支持UnionID機制)
詳情請參考微信開發文檔
網頁鏈接
php微信開發獲取用戶信息
步驟一:
獲取用戶信息需要」通過微信認證「 請確認你是否有相應許可權
步驟二:
獲取用戶基本信息,必須提供ACCESS_TOKEN和openid 兩個參數;id=mp1421140839
//正常情況下,微信會返回下述JSON數據包給公眾號:
{
“subscribe”: 1,
“openid”: “o6_bmjrPTlm6_2sgVt7hMZOPfL2M”,
“nickname”: “Band”,
“sex”: 1,
“language”: “zh_CN”,
“city”: “廣州”,
“province”: “廣東”,
“country”: “中國”,
“headimgurl”: “
eMsv84eavHiaiceqxibJxCfHe/0″,
“subscribe_time”: 1382694957,
“unionid”: ” o6_bmasdasdsad6_2sgVt7hMZOPfL”
“remark”: “”,
“groupid”: 0,
“tagid_list”:[128,2]
}
步驟三:
獲取access_token 前需要配置IP白名單和相應設置讓其有正確的訪問許可權
獲取access_token 需要三個參數
以下是成功案例:
;id=mp1421140183
php手機端怎麼獲取微信openid
//***方法一
獲取code
這裡是你的公眾號的APPIDredirect_uri=;response_type=codescope=snsapi_userinfostate=123#wechat_redirect
用戶點擊確認登錄,自動跳轉下面地址得到code
這個是你自己的跳轉地址
;state=123
後面的這個 ?code=……123 是微信自動跳轉添加的,不是你自己加的
下面是PHP語言,寫在getcode這個頁面里
$code = $_GET[‘code’];//獲取code
$weixin = file_get_contents(“這裡是你的APPIDsecret=這裡是你的SECRETcode=”.$code.”grant_type=authorization_code”);//通過code換取網頁授權access_token
$jsondecode = json_decode($weixin); //對JSON格式的字元串進行編碼
$array = get_object_vars($jsondecode);//轉換成數組
$openid = $array[‘openid’];//輸出openid
//***方法二
$appid = “公眾號在微信的appid”;
$secret = “公眾號在微信的app secret”;
$code = $_GET[“code”];
$get_token_url = ”.$appid.’secret=’.$secret.’code=’.$code.’grant_type=authorization_code’;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$get_token_url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
$json_obj = json_decode($res,true);
//根據openid和access_token查詢用戶信息
$access_token = $json_obj[‘access_token’];
$openid = $json_obj[‘openid’];
$get_user_info_url = ”.$access_token.’openid=’.$openid.’lang=zh_CN’;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$get_user_info_url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
//解析json
$user_obj = json_decode($res,true);
$_SESSION[‘user’] = $user_obj;
print_r($user_obj);
如何用 php 抓取微信文章正文
在剛入手PHP的時候,經理讓我自己做一個文章的管理系統為了方便管理微信端發的消息。除了簡單的添加分類、管理分類、添加文章、管理文章,還有一個功能就是要從微信文章網址中拿到網址上的標題、作者以及發表時間。
一、 頁面使用ajax
把微信頁面的地址放在文章鏈接里,點擊獲取,調用ajax。不說了,直接上代碼td文章鏈接:/td
tdinput type=”text” id=”url” name=”url” size=”50″ input type=”button” value=”獲取” onclick=”javascript:return getMsg()”//td
複製代碼
function getMsg(){
var title=document.getElementById(“title”);var url=document.getElementById(“url”).value;var typeId;
var writer=document.getElementById(“writer”);var addtime=document.getElementById(“addtime”);//alert(url);die;
$.ajax({
url: ‘/addarticle/getmsg’,
type: ‘POST’,
dataType:’text’,
data: ‘url=’+url,
success: function(data)
{
var str=data;
var arr=new Array();//0是標題;1是時間;2是作者var result=str.split(“”);
for(var i=0;iresult.length;i++){
arr.push(result[i]);
}
title.value=arr[0];
addtime.value=arr[1];
writer.value=arr[2];
//alert(arr[0]);
},
error: function()
{
alert(‘請求錯誤’);
}
});
}
複製代碼
二、在php頁面利用file_get_content函數獲取頁面全部信息該函數是將頁面中的所有內容寫在字元串中,想要拿到指定的內容,就用到了正則匹配。正則表達式的相關知識就不說了,直接說今天的內容。將結果放在同一個變數中,用特殊符號隔開,以便在視圖頁面進行分割並寫入文本框內。
複製代碼
public function actionGetmsg(){
$result=array();
$url=$_POST[“url”];
$wx_content=file_get_contents($url);//利用函數獲得網址的內容$title_html=”/.*?title(.*?)\/title.*?/”;//正則匹配文章的標題preg_match($title_html, $wx_content, $matchs);echo $matchs[1].””;
//echo ‘pre’;var_dump($matchs);echo ‘/pre’;//正則匹配文章的添加時間
$creattime_html=”/.*?em id=\”post-date\” class=\”rich_media_meta rich_media_meta_text\”(.*?)\/em.*?/”;preg_match($creattime_html, $wx_content, $matchs);// echo ‘pre’;var_dump($matchs);echo ‘/pre’;echo $matchs[1].””;
//正則匹配文章的作者
$wxh_html=”/.*?a class=\”rich_media_meta rich_media_meta_link rich_media_meta_nickname\” href=\”##\” id=\”post-user\”(.*?)\/a.*?/”;preg_match($wxh_html, $wx_content, $matchs);// echo ‘pre’;var_dump($matchs);echo ‘/pre’;echo $matchs[1].””;
}
複製代碼
三、同時大家可以看到圖片上時間那一欄,可以自己添加這裡使用的是一個小控制項,也就是一個js,WdatePicker.js。
首先在頁面的頭部引入js控制項,在文本框中寫下以下代碼,點擊文本框就可以看到日曆形式的出現,選擇你需要的日期。
tdinput type=”text” name=”newsTime” id=”addtime” onclick=”WdatePicker({dateFmt:’yyyy:MM:dd HH:mm:ss’})” placeholder=”請輸入文章發布時間” / /td
具體的WdatePicker.js可以在網上找一個。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/191174.html