PHP微信公衆番号開発の:accessの獲得とキャッシュtoken,原理とコード
4460 ワード
公衆番号の開発ではaccessが必要なインタフェースがたくさんあることを知っています.tokenはアクセスできますが、毎回accessを使用する必要がある場合はtokenの場合、appidとappsecretでaccess_を取得します.token、またいくつかの資源の浪費、結局、すべてのaccess_tokenの有効期間は7200秒なのでaccess_tokenはファイルを保存することでキャッシュされ、毎回accessを使用する必要があります.tokenのときに直接ファイルを読み取り、tokenが期限切れになっていない場合は、微信サーバに新しいaccessを要求しません.token、これで便利です.結局、ユーザーに注目することが多い場合、2000回のリクエスト回数が足りません.
ターゲット:取得するaccess_token保存先propertiesファイルでaccessを要求するたびにtokenの場合、propertiesファイルを優先的に読み取り、tokenが期限切れであるか否かを判断し、期限切れである場合は、再び微信サーバが要求を送信してaccess_を取得したいtoken、そうでなければ、ファイルに保存されているaccess_に直接戻ります.token.
まず、Propertiesです.phpファイルクラスのコード.Propertiesクラスは、従来のpropertiesファイルと同様に、属性を読み取り、変更する方法を提供します.各属性には「=」で区切られた行が設定されています.以下のようになります.
サーバの同じディレクトリの下でtokenを作成します.phpファイルです.getリクエストを受信してtokenを返します.コードは次のとおりです.
同様に、現在のディレクトリの下にフォルダconfを新規作成し、その中にtokenを新規作成する.confファイル、毎回access_を使用する必要がありますtokenの時に直接tokenにアクセスします.phpファイルでいいです.
ターゲット:取得するaccess_token保存先propertiesファイルでaccessを要求するたびにtokenの場合、propertiesファイルを優先的に読み取り、tokenが期限切れであるか否かを判断し、期限切れである場合は、再び微信サーバが要求を送信してaccess_を取得したいtoken、そうでなければ、ファイルに保存されているaccess_に直接戻ります.token.
まず、Propertiesです.phpファイルクラスのコード.Propertiesクラスは、従来のpropertiesファイルと同様に、属性を読み取り、変更する方法を提供します.各属性には「=」で区切られた行が設定されています.以下のようになります.
_path = fopen($_file, "r+") or exit("Unable to open file!");
stream_set_timeout($this->_path, 0);
$this->_properties = array();
if( flock($this->_path, LOCK_EX) ){//
while(!feof($this->_path))
{
$keywords = fgets($this->_path);
$line = preg_replace('/
|\r
/','',$keywords);
if($line != ''){
$key_value = explode('=', $line);
$this->_properties[] = $key_value;
}
}
ftruncate($this->_path,0); //
rewind($this->_path); //
flock($this->_path, LOCK_UN); //
}
}
//
public function getProperty($_key){
$exist = FALSE;
foreach ($this->_properties as $val) {
if($val[0] == $_key){
$exist = TRUE;
return $val[1];
}
}
if(!$exist){
return 'NO EXISTS.';
}
}
//
public function setProperty($_key, $_value){
$exist = FALSE;
for($_i=0; $_i_properties); $_i++){
$_item = $this->_properties[$_i];
if($_item[0] == $_key){
$exist = TRUE;
$_item[1] = $_value;
$this->_properties[$_i] = $_item;
}
}
if(!$exist){
$this->_properties[] = array($_key, $_value);
return TRUE;
}
}
//
public function saveFile(){
$contents = '';
foreach ($this->_properties as $val) {
$_line = implode('=', $val);
$contents = $contents.$_line."\r
";
}
fwrite($this->_path, $contents);
fclose($this->_path);
}
}
?>
サーバの同じディレクトリの下でtokenを作成します.phpファイルです.getリクエストを受信してtokenを返します.コードは次のとおりです.
AppId . "&secret=" . $this->AppSecret;
// getCurl https
$data = getCurl($url);
//
$resultArr = json_decode($data, true);
// access_token
return $resultArr["access_token"];
}
// token
public function getToken()
{
$tokenFile = __DIR__.'/conf/token.conf';
$prop = new Properties();
$prop->openFile($tokenFile);
$token_way = $prop->getProperty('way');
$token_create = $prop->getProperty('create');
// token , token
if((intval($token_create)+7200) < time()) {
$token = $this -> getAccessToken();
$prop->setProperty('token', $token);
$prop->setProperty('create', time());
}else {
$token = $prop->getProperty('token');
}
$prop->saveFile();
return $token;
}
}
function getCurl($url) {//get https
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$response = [
'code' => 200,
'token' => (new Token())->getToken()
];
echo json_encode($response, TRUE);
?>
同様に、現在のディレクトリの下にフォルダconfを新規作成し、その中にtokenを新規作成する.confファイル、毎回access_を使用する必要がありますtokenの時に直接tokenにアクセスします.phpファイルでいいです.