2015-05-27 91 views
10

我曾經使用PHP類庫連接到谷歌語音發送短信。呼叫會的工作是這樣的:谷歌語音PHP OAuth 2.0

$gv = new GoogleVoice("GmailAccount", "GmailPassword"); 
$gv->sms("PhoneNumber", "TextMsg"); 

它工作得十分完美,直到最近,截至2015年4月20日,谷歌停止支持到谷歌帳戶登錄的老方法。所以我的腳本停止工作,給500錯誤。谷歌表示你必須使用OAuth 2.0進行身份驗證,但是我還沒有找到任何關於如何使用Google語音完成此任務的示例。代碼如下,我沒有寫過,請讓我知道如何調整代碼以使用Google的OAuth系統。

/* 
Version  0.2 
License  This code is released under the MIT Open Source License. Feel  free to do whatever you want with it. 
Author  [email protected], http://www.lostleon.com/ 
LastUpdate 05/28/2010 


Usage: 


*/ 

class GoogleVoice 
{ 
    public $username; 
    public $password; 
    public $status; 
    private $lastURL; 
    private $login_auth; 
    private $inboxURL = 'https://www.google.com/voice/m/'; 
    private $loginURL = 'https://www.google.com/accounts/ClientLogin'; 
    private $smsURL = 'https://www.google.com/voice/m/sendsms'; 

public function __construct($username, $password) 
{ 
    $this->username = $username; 
    $this->password = $password; 
} 

public function getLoginAuth() 
{ 
    $login_param = "accountType=GOOGLE&Email={$this->username}&Passwd={$this->password}&service=grandcentral&source=com.lostleon.GoogleVoiceTool"; 
    $ch = curl_init($this->loginURL); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20"); 
    curl_setopt($ch, CURLOPT_REFERER, $this->lastURL); 
    curl_setopt($ch, CURLOPT_POST, "application/x-www-form-urlencoded"); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $login_param); 
    $html = curl_exec($ch); 
    $this->lastURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 
    curl_close($ch); 
    $this->login_auth = $this->match('/Auth=([A-z0-9_-]+)/', $html, 1); 
    return $this->login_auth; 
} 

public function get_rnr_se() 
{ 
    $this->getLoginAuth(); 
    $ch = curl_init($this->inboxURL); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $headers = array("Authorization: GoogleLogin auth=".$this->login_auth, 'User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20'); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    $html = curl_exec($ch); 
    $this->lastURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 
    curl_close($ch); 
    $_rnr_se = $this->match('!<input.*?name="_rnr_se".*?value="(.*?)"!ms', $html, 1); 
    return $_rnr_se; 
} 

public function sms($to_phonenumber, $smstxt) 
{ 
    $_rnr_se = $this->get_rnr_se(); 
    $sms_param = "id=&c=&number=".urlencode($to_phonenumber)."&smstext=".urlencode($smstxt)."&_rnr_se=".urlencode($_rnr_se); 
    $ch = curl_init($this->smsURL); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $headers = array("Authorization: GoogleLogin auth=".$this->login_auth, 'User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20'); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_REFERER, $this->lastURL); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sms_param);  
    $this->status = curl_exec($ch); 
    $this->lastURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 
    curl_close($ch); 
    return $this->status; 
} 

private function match($regex, $str, $out_ary = 0) 
{ 
    return preg_match($regex, $str, $match) == 1 ? $match[$out_ary] : false; 
} 
} 
+0

切換到Oauth2,刪除登錄和密碼的東西。 – DaImTo

+0

不像移動到Oauth2那樣簡單。找不到Googlevoice的API(在使用Oath2時需要使用「範圍」變量)。歡迎提供其他建議 –

回答

5

我會送你到下一個答案: https://stackoverflow.com/a/4131915/2992810

不幸的谷歌語音已經改變了他們的API,所以你不能用它了。 https://github.com/aaronpk/Google-Voice-PHP-API(看看頭部的評論)

谷歌語音不是一個開放的API,所以他們沒有維護它。 不好意思告訴你,但今天以我自己的經驗,短信服務非常便宜,實際上購買服務許可證的費用要低於與Google爭取的服務許可證費用以及API不斷變化,因此,您的網站總是會因此類變化而下降。 認爲你的時間是一種資源,花費你的時間會花費你更多!

0

GV4J是一個能夠登錄到Google語音的Java庫,因此它可能是更新您的PHP代碼以進行身份​​驗證的良好參考。