Exemplo n.º 1
0
    /**
     * 给定 openid 获取指定关注者的信息
     * 每次用户触发交互时更新信息
     * @param string $openID
     */
    private function recordOpenUser($openID)
    {
        if (trim($openID) == '') {
            return;
        }
        $this->getToken();
        $model = M();
        $sql = 'SELECT last_update FROM user_wechat WHERE open_id="' . $openID . '"';
        $f = $model->query($sql);
        $urlFull = self::WX_API_SERVICE_URL . 'user/info?access_token=' . self::$wx_last_token_string . '&openid=' . urlencode($openID);
        if ($f == null) {
            // 记录新粉丝
            $info = Curl::get($urlFull, 2);
            if ($info === false) {
                return;
            }
            $user = json_decode($info['result'], true);
            if (!array_key_exists('openid', $user)) {
                return;
            }
            $sql = 'INSERT INTO `user_wechat` (`open_id`, `subscribe`, `subscribe_time`, `nickname`, `sex`, `language`, `city`, `province`, `country`, `headimgurl`, `user_id`, `user_mobile`, `create_time`) VALUES
			("' . $user['openid'] . '", ' . $user['subscribe'] . ', ' . $user['subscribe_time'] . ', "' . $user['nickname'] . '", ' . $user['sex'] . ', "' . $user['language'] . '", "' . $user['city'] . '", "' . $user['province'] . '", "' . $user['country'] . '", "' . $user['headimgurl'] . '", 0, "0", now())';
            $model->execute($sql);
        }
        // 更新粉丝的最后交互时间
        $sql = 'UPDATE user_wechat SET last_time=now() WHERE open_id="' . $openID . '"';
        $model->execute($sql);
        // 更新粉丝信息
        $lastUpdate = $f[0]['last_update'];
        if ($lastUpdate == null || $lastUpdate == 'null') {
            $info = Curl::get($urlFull, 2);
            if ($info === false) {
                return;
            }
            $user = json_decode($info['result'], true);
            if (!array_key_exists('openid', $user)) {
                return;
            }
            $sql = 'UPDATE user_wechat SET last_time=now(), last_update=now(), nickname="' . $user['nickname'] . '",sex="' . $user['sex'] . '",language="' . $user['language'] . '",city="' . $user['city'] . '",province="' . $user['province'] . '",country="' . $user['country'] . '",headimgurl="' . $user['headimgurl'] . '" WHERE open_id="' . $openID . '"';
            $model->execute($sql);
        } else {
            // 每 7 天更新粉丝信息
            $lastUpdate = strtotime($lastUpdate);
            if ($this->time - $lastUpdate > 86400 * 7) {
                $info = Curl::get($urlFull, 2);
                if ($info === false) {
                    return;
                }
                $user = json_decode($info['result'], true);
                if (!array_key_exists('openid', $user)) {
                    return;
                }
                $sql = 'UPDATE user_wechat SET last_time=now(), last_update=now(), nickname="' . $user['nickname'] . '",sex="' . $user['sex'] . '",language="' . $user['language'] . '",city="' . $user['city'] . '",province="' . $user['province'] . '",country="' . $user['country'] . '",headimgurl="' . $user['headimgurl'] . '" WHERE open_id="' . $openID . '"';
                $model->execute($sql);
            }
        }
    }
    /**
     * API - 批量拉取/更新全部关注者信息列表
     * 每日运行一次,增加或者更新用户信息
     * http://test.api.4001002003.com/wechat/?method=get-users&ver=1.0&time=1386582783&hash=90fbad28c54e783dcfd01723c01171cb
     */
    private function apiGetUsers()
    {
        $this->getToken();
        $url = self::WX_API_SERVICE_URL . 'user/get?access_token=' . self::$wx_last_token_string;
        $result = Curl::get($url, 2);
        if ($result === false) {
            return;
        }
        $resp = json_decode($result['result'], true);
        if (!array_key_exists('data', $resp)) {
            return;
        }
        $ids = $resp['data']['openid'];
        echo count($ids);
        $i = 1;
        $model = M();
        foreach ($ids as $openID) {
            $urlFull = self::WX_API_SERVICE_URL . 'user/info?access_token=' . self::$wx_last_token_string . '&openid=' . urlencode($openID);
            $info = Curl::get($urlFull, 2);
            if ($info === false) {
                continue;
            }
            $user = json_decode($info['result'], true);
            if (!array_key_exists('openid', $user)) {
                continue;
            }
            $sql = 'INSERT INTO `user_wechat` (`open_id`, `subscribe`, `subscribe_time`, `nickname`, `sex`, `language`, `city`, `province`, `country`, `headimgurl`, `user_id`, `user_mobile`, `create_time`, `comment`) VALUES
			("' . $openID . '", ' . $user['subscribe'] . ', ' . $user['subscribe_time'] . ', "' . $user['nickname'] . '", ' . $user['sex'] . ', "' . $user['language'] . '", "' . $user['city'] . '", "' . $user['province'] . '", "' . $user['country'] . '", "' . $user['headimgurl'] . '", 0, "0", now(), "自动采集")';
            $f = $model->execute($sql);
            if ($f === false) {
                // 更新已存在的粉丝
                $sql = 'UPDATE user_wechat SET nickname="' . $user['nickname'] . '",sex="' . $user['sex'] . '",language="' . $user['language'] . '",city="' . $user['city'] . '",province="' . $user['province'] . '",country="' . $user['country'] . '",headimgurl="' . $user['headimgurl'] . '" WHERE open_id="' . $openID . '"';
                $f = $model->execute($sql);
            }
            $i++;
        }
    }
Exemplo n.º 3
0
 /**
  * http://127.0.0.1:114/test
  */
 function index()
 {
     $result = Curl::get('http://www.baidu.com', 13);
     dump($result);
 }