86159xxxxxxxx,86134xxxxxxxx)curl 擴展,編碼統一使用 GBK(配合拼音轉換庫)project/
├── libs/
│ ├── pinyin.php # 中文轉拼音核心庫
│ └── googlevoice.class.php # Google Voice API 封裝類
└── send_sms.php # 業務調用示例
<?php
/**
* Google Voice 短信發送示例
* 需啟用 curl 擴展,文件編碼為 GBK
*/
require_once 'libs/pinyin.php';
require_once 'libs/googlevoice.class.php';
// 配置帳號
$username = 'your_account@gmail.com';
$password = 'your_password';
// 接收號碼(多號碼用英文逗號分隔)
$sendto = '8615900000000,8613400000000';
// 短信內容(支援中文,自動轉拼音)
$message = '我是旋風'; // 實際發送內容為 "Wo Shi Xuan Feng"
// 實例化並發送
$sms = new GoogleVoice($username, $password);
$result = $sms->send($sendto, $message);
// 輸出結果:成功返回 "Text sent to 86134..." 類似字串
echo $result;
?>
class GoogleVoice {
private $username, $password, $cookie;
public function __construct($username, $password) {
$this->username = $username;
$this->password = $password;
$this->login(); // 自動登錄獲取 cookie
}
public function send($to, $message) {
// 內部實現:模擬登錄 → 獲取 _rnr_se token → POST 發送
// 返回 Google 原始響應文本
}
}
pinyin.php 採用字典映射表,支援常用漢字。轉換規則:
注意:此功能屬「娛樂性質」,生產環境建議改用 Unicode 碼點或 Base64 編碼傳遞中文,或尋支援 UTF-8 的替代通道。
| 現象 | 可能原因 | 解決方案 |
|---|---|---|
| 返回 "Need Authentication" | Cookie 失效或登錄失敗 | 檢查帳號密碼、IP 是否被 Google 限制、是否需驗證碼 |
| 發送無響應 | curl 超時或被牆 | 設置 CURLOPT_PROXY、增加 CURLOPT_TIMEOUT |
| 中文顯示亂碼 | 文件編碼非 GBK | 確保 PHP 文件另存為 GBK/ANSI,或修改庫內編碼轉換邏輯 |
完整源碼打包下載:GoogleVoiceApi.rar(含示例、庫文件、說明文檔)