<?php
/*获取access_token,不能用于获取用户信息的token*/
function getAccessToken()
{
$appid = 'AppId';
$secret = 'AppSecret';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
return $res;
exit();
}
//图片合法性验证
function http_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
return $output;
exit();
}
// 获取手机号
function getPhoneNumber(){
$tmp = getAccessToken();
$tmptoken = json_decode($tmp);
$token = $tmptoken->access_token;
$data['code'] = $_GET['code'];//前端获取code
$url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=$token";
$info = http_request($url,json_encode($data),'json');
// 一定要注意转json,否则汇报47001错误
$tmpinfo = json_decode($info);
$code = $tmpinfo->errcode;
$phone_info = $tmpinfo->phone_info;
//手机号
$phoneNumber = $phone_info->phoneNumber;
if($code == '0'){
echo json_encode(['code'=>1,'msg'=>'请求成功','phoneNumber'=>$phoneNumber]);
die();
}else{
echo json_encode(['code'=>2,'msg'=>'请求失败']);
die();
}
}
getPhoneNumber();
?>
PHP阿里云OSS生成STS_TOKEN,用于JS签名直接上传
<?php
require __DIR__.'/../utils/vendor/autoload.php';
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
function gmt_iso8601($time) {
$dtStr = date("c", $time);
$mydatetime = new DateTime($dtStr);
$expiration = $mydatetime->format(DateTime::ISO8601);
$pos = strpos($expiration, '+');
$expiration = substr($expiration, 0, $pos);
return $expiration."Z";
}
function oss_signature($id,$key,$host,$expire,$dir,$SecurityToken){
$now = time();
$end = $now + $expire;
$expiration = gmt_iso8601($end);
//最大文件大小.用户可以自己设置
$condition = array(0=>'content-length-range', 1=>0, 2=>1048576000);
$conditions[] = $condition;
//表示用户上传的数据,必须是以$dir开始, 不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录
$start = array(0=>'starts-with', 1=>'$key', 2=>$dir);
$conditions[] = $start;
$arr = array('expiration'=>$expiration,'conditions'=>$conditions);
//echo json_encode($arr);
//return;
$policy = json_encode($arr);
$base64_policy = base64_encode($policy);
$string_to_sign = $base64_policy;
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $key, true));
$response = array();
$response['accessid'] = $id;
$response['host'] = $host;
$response['policy'] = $base64_policy;
$response['signature'] = $signature;
$response['expire'] = $end;
//这个参数是设置用户上传指定的前缀
$response['dir'] = $dir;
$response['secToken'] = $SecurityToken;
return $response;
}
//构建一个阿里云客户端,用于发起请求。
AlibabaCloud::accessKeyClient('AccessKeyId', 'AccessKeySecret')
->regionId('region_id')
->asDefaultClient();
//设置参数,发起请求。
try {
$result = AlibabaCloud::sts()
->v20150401()
->assumeRole()
->withRoleSessionName($client_id)
->withRoleArn($oss_access_role)
->request();
$result = $result->toArray();
echo json_encode($result);
} catch (ClientException $e) {
echo json_encode(array('status'=>'0','message'=>'ERROR_CLIENT'));
} catch (ServerException $e) {
echo json_encode(array('status'=>'0','message'=>'ERROR_SERVER','DATAS'=>$e->getErrorMessage()));
}
?>