ホームページ  >  記事  >  バックエンド開発  >  PHP WeChat開発QRコード生成クラス_PHPチュートリアル

PHP WeChat開発QRコード生成クラス_PHPチュートリアル

WBOY
WBOYオリジナル
2016-07-13 09:48:48795ブラウズ

PHP WeChat開発用のQRコード生成クラス

この記事では主にPHP WeChat開発用のQRコード生成クラスを紹介し、WeChatインターフェースを使用してQRコードの生成を実現し、友達が参照できるサンプルコードを直接提供します。それ

?

/**

* PhpStormによって作成されました

* ユーザー: bin

*日付:15-1-16

* 時刻: 上午9:48

  */

名前空間 HomeCommon;

// WeChat処理クラス

set_time_limit(30);

クラスウェイシン{

//構築方法

static $qrcode_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?";

static $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";

static $qrcode_get_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?";

//QRコードを生成

パブリック関数 getEwm($wechatid,$fqid,$type = 1){

$wechat = M('Member_public')->where(array('id'=> $wechatid))->find();

$appid = $wechat['appid'];

$secret = $wechat['secret'];

$ACCESS_TOKEN = $this->getToken($appid,$secret);

$url = $this->getQrcodeurl($ACCESS_TOKEN,$fqid,1);

return DownLoadQr($url,time());

}

保護された関数 getQrcodeurl($ACCESS_TOKEN,$fqid,$type = 1){

$url = self::$qrcode_url.'access_token='.$ACCESS_TOKEN;

if($type == 1){

//永続的なQRコードを生成します

$qrcode= '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$fqid.'}}}';

}その他{

//一時的なQRコードを生成します

$qrcode = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$fqid.'}}}';

}

$result = $this->http_post_data($url,$qrcode);

$oo = json_decode($result[1]);

if(!$oo->ticket){

$this->ErrorLogger('getQrcodeurl が失敗しました。エラー情報: getQrcodeurl の取得に失敗しました');

exit();

}

$url = self::$qrcode_get_url.'ticket='.$oo->ticket.'';

$url を返す;

}

保護された関数 getToken($appid,$secret){

$ACCESS_TOKEN = file_get_contents(self::$token_url."appid=$appid&secret=$secret");

$ACCESS_TOKEN = json_decode($ACCESS_TOKEN);

$ACCESS_TOKEN = $ACCESS_TOKEN->access_token;

$ACCESS_TOKENを返す;

}

保護された関数 http_post_data($url, $data_string) {

$ch =curl_init();

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(

)

'Content-Type: application/json',

'Content-Length: ' .strlen($data_string))

);

ob_start();

curl_exec($ch);

if (curl_errno($ch)) {

$this->ErrorLogger('curl が失敗しました。エラー情報: '.curl_error($ch));

}

$return_content = ob_get_contents();

ob_end_clean();

$return_code =curl_getinfo($ch, CURLINFO_HTTP_CODE);

配列を返す($return_code, $return_content);

}

//QRコードをサーバーにダウンロードします

保護された関数 DownLoadQr($url,$filestring){

if($url == ""){

false を返す;

}

$filename = $filestring.'.jpg';

ob_start();

readfile($url);

$img=ob_get_contents();

ob_end_clean();

$size=strlen($img);

$fp2=fopen('./Uploads/qrcode/'.$filename,"a");

if(fwrite($fp2,$img) === false){

$this->ErrorLogger('dolwload image failed. Error Info: Unable to write image');

exit();

}

fclose($fp2);

return './Uploads/qrcode/'.$filename;

}

プライベート関数ErrorLogger($errMsg){

$logger = fopen('./ErrorLog.txt', 'a+');

fwrite($logger, date('Y-m-d H:i:s')." エラー情報 : ".$errMsg."rn");

}

}

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/1022060.html技術記事この記事では、PHP WeChat 開発の QR コード生成クラスを主に紹介します。この記事では、WeChat インターフェイスを使用して QR コードの生成を実現し、サンプル コードを直接提供します。
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。