public function testAction() { /** * 知乎发现首页 */ $curl = new Curl('https://www.baidu.com'); $cookie = 'a23155b7c4337bbfe54acf8773b21|1451283378000|1451283378000; _za=a13ca887-021b-45da-88fe-c1b63982374b; _xsrf=33bdad31147bf00b064b50ec10f9976a; cap_id="NGVjMGYzMDIwNzlkNDVlYmE2NzZhNjQ1MzEyZGE1MDQ=|1451377826|61acc755db19be73bdcacd852fa1a97a1c8b6821"; z_c0="QUJEQ1p2REhLZ2dYQUFBQVlRSlZUYmZScVZhXzdNbl9MczFjd081Z3hQSjJHY0RoWG5GMmpRPT0=|1451377847|76fdc816777745015b2c1250b0a2fd190cefa84a"; __utma=51854390.1352335625.1451283386.1451351537.1451368990.3; __utmc=51854390; __utmz=51854390.1451351537.2.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __utmv=51854390.100--|2=registration_date=20150601=1^3=entry_date=20150601=1'; $curl->getMethod($cookie); $zhihuContent = $curl->exec(); print_r($curl->getResponseData()); var_dump($zhihuContent); exit; $zhihuStr = preg_replace("/\n/", ' ', $zhihuContent); preg_match_all('/<div class=\\"explore-tab\\" id=\\"js-explore-tab\\">.*<div class=\\"zu-main-sidebar\\">/', $zhihuStr, $out1); preg_match_all('/<h2>(.*?)<\\/h2>/', $out1[0][0], $out2); //print_r($out2);exit; $html = new simple_html_dom(); $data = array(); foreach ($out2[0] as $key => $val) { $dom = $html->load($val); $data[$key]['title'] = $dom->find("a", 0)->innertext; $data[$key]['url'] = 'https://www.zhihu.com' . $dom->find("a", 0)->href; } print_r($data); exit; }
/** * 回复客服消息,消息使用json格式化 * * @param json $params */ public function reply($params) { $host = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s"; $url = sprintf($host, $this->get_accesstoken()); $info = \Core\Curl::post($url, $params); return json_decode($info->response, true); }
/** * 上传文件 * @param string $type * @param string $fileRealPath */ public function upload($type = 'image', $fileRealPath) { $types = array('image', 'voice', 'video', 'file'); if (in_array($type, $types)) { $host = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s"; $url = sprintf($host, $this->get_accesstoken('access_token'), $type); if (version_compare(PHP_VERSION, '5.5.0', '<')) { $data = array('media' => "@{$fileRealPath}"); } else { $fileName = basename($fileRealPath); $finfo = finfo_open(FILEINFO_MIME_TYPE); $mimeType = finfo_file($finfo, $fileRealPath); $data['media'] = new \CurlFile($fileRealPath, $mimeType, $fileName); } $info = \Core\Curl::post($url, $data); $response = json_decode($info->response, true); if (!empty($response['media_id'])) { return $response['media_id']; } if (!empty($response['thumb_media_id'])) { return $response['thumb_media_id']; } } else { throw new \Exception('upload file type is not vaild!'); } return FALSE; }
/** * 发送消息请求 * @param string $access_token * @param json $message */ protected function push($message) { $host = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s'; $info = \Core\Curl::post(sprintf($host, $this->get_accesstoken()), $message); $respone = json_decode($info->response, true); if (isset($respone['errcode']) && $respone['errcode'] == 0) { return $respone; } return FALSE; }
/** * 获取菜单 */ public function get() { $host = 'https://api.weixin.qq.com/cgi-bin/menu/get?access_token=%s'; $url = sprintf($host, $this->get_accesstoken()); $info = \Core\Curl::get($url); $response = json_decode($info->response, true); if ($response['errcode'] == 0) { return $response; } return false; }
/** * 获取用户授权的token * @param string $code * @param int $corp_agent_id */ public function get_userid($code) { $host = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=%s&code=%s&agentid=%s"; $url = sprintf($host, $this->get_accesstoken(), $code, $this->corpagentid); $info = \Core\Curl::get($url); $response = json_decode($info->response, true); if (isset($response['UserId'])) { return $response['UserId']; } return false; }
/** * 获取coder话题对应页数的title和url * @param $pageNum int 第几页 */ public function _getTitle($pageNum) { $curl = new Curl("https://www.zhihu.com/topic/19552330/top-answers?page={$pageNum}"); $cookie = 'f8773b21|1451283378000|1451283378000; _za=a13ca887-021b-45da-88fe-c1b63982374b; _xsrf=33bdad31147bf00b064b50ec10f9976a; cap_id="NGVjMGYzMDIwNzlkNDVlYmE2NzZhNjQ1MzEyZGE1MDQ=|1451377826|61acc755db19be73bdcacd852fa1a97a1c8b6821"; z_c0="QUJEQ1p2REhLZ2dYQUFBQVlRSlZUYmZScVZhXzdNbl9MczFjd081Z3hQSjJHY0RoWG5GMmpRPT0=|1451377847|76fdc816777745015b2c1250b0a2fd190cefa84a"; __utma=51854390.1352335625.1451283386.1451351537.1451368990.3; __utmc=51854390; __utmz=51854390.1451351537.2.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __utmv=51854390.100--|2=registration_date=20150601=1^3=entry_date=20150601=1'; $curl->getMethod($cookie); $zhihuContent = $curl->exec(); //变成一行 $zhihuStr = preg_replace("/\n/", ' ', $zhihuContent); preg_match_all('/<div class=\\"feed-item feed-item-hook folding\\" .*<div class=\\"zm-invite-pager\\">/', $zhihuStr, $out1); preg_match_all('/<h2>(.*?)<\\/h2>/', $out1[0][0], $out2); $data = array(); foreach ($out2[0] as $key => $val) { preg_match_all('/href=\\"(.*?)\\">/', $val, $tmp1, PREG_SET_ORDER); preg_match_all('/>(.*?)</', $val, $tmp2); $data[$key]['title'] = $tmp2[1][1]; $data[$key]['url'] = 'https://www.zhihu.com' . $tmp1[0][1]; } $excellentHandle = new ZhihuCoderExcellentTitleHandle(); $res = $excellentHandle->insert($data); var_dump($res); }
/** * 获取用户基本信息 * @param string $access_token * @param string $open_id */ public function user_info($key = null) { if (empty($this->user_info)) { $host = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN"; $url = sprintf($host, $this->user_accesstoken['access_token'], $this->user_accesstoken['openid']); $info = \Core\Curl::get($url); $response = json_decode($info->response, true); if (isset($response['nickname'])) { $this->user_info = $response; } } if (!is_null($key) && isset($this->user_info[$key])) { return $this->user_info[$key]; } return $this->user_info; }
private function get_jsapi_ticket($key = 'ticket', $default = null) { \Core\Cache::init_config_params(); $this->ticket = \Core\Cache::get('jsapi_ticket'); if (empty($this->ticket)) { $host = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=%s"; $url = sprintf($host, $this->get_accesstoken()); $info = \Core\Curl::get($url); $respone = json_decode($info->response, true); if (isset($respone['ticket'])) { $this->ticket = $respone; } \Core\Cache::set('jsapi_ticket', $respone); } if (!isset($this->ticket[$key])) { $this->ticket[$key] = $default; } return $this->ticket[$key]; }
/** * 核销卡券 * @param $card_id 卡券ID * @param $code 自定义编码 */ public function consumption_card($card_id, $code = '') { $host = 'https://api.weixin.qq.com/card/code/consume?access_token=%s'; $url = $url = sprintf($host, $this->get_accesstoken()); $params = array('card_id' => $card_id); if (!empty($code)) { $params['code'] = $code; } $info = \Core\Curl::post($url, json_encode($params)); $response = json_decode($info->response, true); log_message(var_export($response, true), var_export($params, true)); if ($response['errcode'] == 0) { return $response; } return false; }
/** * 获取关注用户的信息,未关注用户获取不到个人信息 */ public function subscribe_user_info($open_id) { $host = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN"; $url = sprintf($host, $this->get_accesstoken(), $open_id); $info = \Core\Curl::get($url); $response = json_decode($info->response, true); if (!empty($response['subscribe'])) { return $response; } return NULL; }
/** * 请求token */ public function get_accesstoken($key = 'access_token', $default = null) { \Core\Cache::init_config_params(); $this->enterprise_accesstoken = \Core\Cache::get('enterprise_accesstoken'); if (empty($this->enterprise_accesstoken)) { $host = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s'; $url = sprintf($host, $this->corpid, $this->corpsecret); $info = \Core\Curl::get($url); $respone = json_decode($info->response, true); if (isset($respone['access_token'])) { $this->enterprise_accesstoken = $respone; } \Core\Cache::set('enterprise_accesstoken', $respone); } if (!isset($this->accesstoken[$key])) { $this->accesstoken[$key] = $default; } return $this->accesstoken[$key]; }
/** * 发送请求 */ public function send() { $host = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; $option = array(CURLOPT_SSLCERT => $this->cert, CURLOPT_SSLKEY => $this->key, CURLOPT_CAINFO => $this->ca, CURLOPT_HTTPHEADER => array('Content-Type: text/xml')); $xml = $this->create_xml(); $rs = \Core\Curl::post($host, $xml, $option); $message = $this->xml2arr($rs->response); log_message(var_export($message, true)); return $message; }
/** * 获取未关注用户 * @param int $department_id * @param int $fetch_child */ public function get_unfollow_members($department_id = 1, $fetch_child = 1) { $host = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=%s&department_id=%s&fetch_child=%s"; $url = sprintf($host, $this->get_accesstoken(), $department_id, $fetch_child); $info = \Core\Curl::get($url); $respone = json_decode($info->response, true); if (isset($respone['userlist'])) { return $respone; } return FALSE; }