wechatqrcode.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Created by 上海风车教育科技有限公司.
  4. * User: 刘红伟
  5. * Date: 15-11-25
  6. * Email: 454303753@qq.com
  7. * File:wechatqrcode.php
  8. */
  9. class wechatqrcode{
  10. private $url;
  11. private $token;
  12. private $json_data;
  13. public function __construct($token,$scene_str){
  14. $this->token=$token;
  15. $this->setUrl();
  16. $this->setVal($scene_str);
  17. }
  18. public function setUrl(){
  19. $url="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->token;
  20. $this->url=$url;
  21. }
  22. public function create(){
  23. $http = wx_http::factory($this->url, wx_http::TYPE_CURL);
  24. $json = $http->post($this->url, $this->json_data, array(
  25. 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)',
  26. 'Content-Type: application/json; charset=utf-8',
  27. 'Content-Length: '. strlen($this->json_data)
  28. ));
  29. $result = json_decode($json,true);
  30. if ((isset($result['errcode']) ) OR (isset($result['errmsg'])))
  31. {
  32. $this->_errno = $result['errcode'];
  33. $this->_error = $result['errmsg'];
  34. return FALSE;
  35. }
  36. return $this->getTicket($result['ticket']);
  37. }
  38. public function setVal($scene_str){
  39. $data=array();
  40. $data['action_name']='QR_LIMIT_STR_SCENE';
  41. $data['action_info']=array('scene'=>array('scene_str'=>$scene_str));
  42. $this->json_data = json_encode($data);
  43. }
  44. protected function getTicket($ticket){
  45. return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
  46. }
  47. }