加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
gzh.php 3.40 KB
一键复制 编辑 原始数据 按行查看 历史
司空见惯 提交于 2024-06-26 10:01 . 微信公众号的有关项目
<?php
class GZH
{
private $appid = 'wx3344dd253dbeabac';
private $secret = 'c4d4bf1d7f92fb4d11e44bee83478e2b';
public function get(){
$appid = 'wx3344dd253dbeabac';
$secret = 'c4d4bf1d7f92fb4d11e44bee83478e2b';
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
// print_r($url);
$res = $this->curlRequest('GET',$url,'form');
// print_r($res['access_token']);
if (!empty($res['errcode'])){
return false;
}
// print_r($res['access_token']);
return $res['access_token'];
}
public function getWxIp()
{
$token=$this->get();
if (!$token){
return false;
}
$url = "https://api.weixin.qq.com/cgi-bin/get_api_domain_ip?access_token=$token";
$c=$this->curlRequest('GET',$url,'form');
return $c;
}
// CURL
private function curlRequest($type, $url, $contentType, $data = [], $timeout = 60)
{
try {
$type = strtoupper($type);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url); // 设置请求链接
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 不直接输出页面
curl_setopt($curl, CURLOPT_HEADER, 0); // 获取响应头向下
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); // 请求超时时间,单位:秒
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1');
if (substr($url, 0, 5) == 'https') {
// 自动判断是否是 https 提交
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
// https请求,不验证证书和 hosts
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
}
// 判断 Content-Type 类型
if ($contentType == 'json') {
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
}
if ($contentType == 'form') {
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type:application/x-www-form-urlencoded']);
}
switch ($type) {
case "GET" :
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
break;
case "POST":
curl_setopt($curl, CURLOPT_POST, true);
if ($contentType == 'json') {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
}
if ($contentType == 'form') {
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
}
break;
}
$result = curl_exec($curl);
$err_code = curl_errno($curl);
$curlInfo = curl_getinfo($curl);
curl_close($curl);
if ($err_code) {
return ['status' => 'error', 'msg' => 'sendRequestError:' . $err_code];
}
if ($curlInfo['http_code'] == 200) {
return json_decode($result, true);
}
return false;
} catch (\Exception $e) {
return ['status' => 'error', 'msg' => $e->getMessage()];
}
}
}
$a = new GZH();
print_r($a->getWxIp());
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化