第一步:获取海外手机号卡
首先需要获取一个海外手机号码,最好是实体sim卡。
比如使用光年号平台( https://gnhao.com ),购买一个英国的手机号卡。
第二步:配置转发通道
在光年号平台里面配置短信转发通道,可以指定自己的英国手机号转发到自己的手机号。
第三步:测试转发是否生效
使用一些注册系统尝试接收短信验证码,然后看下光年号后台是否收到短信,自己的手机号是否收到短信。
配置API实现更灵活的短信转发
如果需要更加灵活的转发方案,可以使用光年号的api功能,通过光年号短信API结合阿里云短信推送,可以实现各种复杂的短信接收和转发服务。
下面是一个示例代码:
<?php
/**
* 短信转发服务:光年号收信 + 阿里云转发
*/
class SmsForwarder {
// 光年号API配置
private $gnApiKey = '你的光年号API_KEY';
private $gnApiUrl = 'https://gnhao.com/index/api/get_sms_v1';
// 阿里云短信配置
private $aliAccessKeyId = '你的阿里云AccessKeyId';
private $aliAccessSecret = '你的阿里云AccessKeySecret';
private $aliSignName = '你的短信签名';
private $aliTemplateCode = '你的模板CODE';
private $targetPhone = '要转发的目标手机号';
// 转发最新短信
public function forwardLatestSms() {
try {
// 1. 从光年号获取最新短信
$sms = $this->getGnSms();
if (empty($sms)) {
echo "没有新短信\n";
return false;
}
// 2. 通过阿里云转发
$content = "发信人:{$sms['sender']}\n内容:{$sms['content']}\n时间:{$sms['send_time']}";
$result = $this->sendAliSms($content);
echo "转发成功! 阿里云返回: " . json_encode($result) . "\n";
return true;
} catch (Exception $e) {
echo "转发失败: " . $e->getMessage() . "\n";
return false;
}
}
// 获取光年号短信
private function getGnSms() {
$params = ['app_key' => $this->gnApiKey];
$response = $this->httpPost($this->gnApiUrl, $params);
if ($response['code'] != 200) {
throw new Exception("光年号API错误: {$response['message']}");
}
return $response['data'] ?? [];
}
// 发送阿里云短信
private function sendAliSms($content) {
$params = [
'PhoneNumbers' => $this->targetPhone,
'SignName' => $this->aliSignName,
'TemplateCode' => $this->aliTemplateCode,
'TemplateParam' => json_encode(['content' => $content])
];
// 阿里云API需要特殊签名处理
$apiParams = $this->createAliApiParams($params);
$response = $this->httpPost('https://dysmsapi.aliyuncs.com', $apiParams);
if ($response['Code'] != 'OK') {
throw new Exception("阿里云API错误: {$response['Message']}");
}
return $response;
}
// 阿里云API签名生成
private function createAliApiParams($params) {
$publicParams = [
'SignatureMethod' => 'HMAC-SHA1',
'SignatureNonce' => uniqid(),
'AccessKeyId' => $this->aliAccessKeyId,
'SignatureVersion' => '1.0',
'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
'Format' => 'JSON',
'Version' => '2017-05-25',
'Action' => 'SendSms',
'RegionId' => 'cn-hangzhou'
];
$params = array_merge($publicParams, $params);
ksort($params);
$queryString = '';
foreach ($params as $key => $value) {
$queryString .= '&' . rawurlencode($key) . '=' . rawurlencode($value);
}
$stringToSign = 'POST&%2F&' . rawurlencode(substr($queryString, 1));
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $this->aliAccessSecret . '&', true));
$params['Signature'] = $signature;
return $params;
}
// 通用HTTP POST请求
private function httpPost($url, $params) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($params),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('HTTP请求失败: ' . curl_error($ch));
}
curl_close($ch);
$result = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('JSON解析失败: ' . json_last_error_msg());
}
return $result;
}
}
// 使用示例
$forwarder = new SmsForwarder();
$forwarder->forwardLatestSms();
// 定时执行示例(如需要)
// while (true) {
// $forwarder->forwardLatestSms();
// sleep(10); // 每10秒检查一次
// }
?>