Unmaintaned: Please do not contact me with support requests. If this no longer works, it's because Google Voice changed their systems. I will accept pull requests that fix things. Check out http://twilio.com for a much better API for sending and receiving SMSs.
Two suggested additions/revision. The first removes references to Gizmo (RIP) and allows canceling a call in progress (replaces existing callNumber function and adds cancelCall function):
/**
* Place a call to $number connecting first to $fromNumber.
* @param $number The 10-digit phone number to call (formatted with parens and hyphens or none).
* @param $fromNumber The 10-digit number on your account to connect the call (no hyphens or spaces).
* @param $phoneType (mobile, work, home) The type of phone the $fromNumber is. The call will not be connected without this value.
*/
public function callNumber($number, $from_number, $phone_type = 'mobile') {
$types = array(
'mobile' => 2,
'work' => 3,
'home' => 1
);
// Make sure phone type is set properly.
if(!array_key_exists($phone_type, $types))
throw new Exception('Phone type must be mobile, work, or home');
// Login to the service if not already done.
$this->_logIn();
// Send HTTP POST request.
curl_setopt($this->_ch, CURLOPT_URL, 'https://www.google.com/voice/call/connect/');
curl_setopt($this->_ch, CURLOPT_POST, TRUE);
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, array(
'_rnr_se' => $this->_rnr_se,
'forwardingNumber' => '+1'.$from_number,
'outgoingNumber' => $number,
'phoneType' => $types[$phone_type],
'remember' => 0,
'subscriberNumber' => 'undefined'
));
curl_exec($this->_ch);
}
/**
* Cancel a call to $number connecting first to $fromNumber.
* @param $number The 10-digit phone number to call (formatted with parens and hyphens or none).
* @param $fromNumber The 10-digit number on your account to connect the call (no hyphens or spaces).
* @param $phoneType (mobile, work, home) The type of phone the $fromNumber is. The call will not be connected without this value.
*/
public function cancelCall($number, $from_number, $phone_type = 'mobile') {
$types = array(
'mobile' => 2,
'work' => 3,
'home' => 1
);
// Make sure phone type is set properly.
if(!array_key_exists($phone_type, $types))
throw new Exception('Phone type must be mobile, work, or home');
// Login to the service if not already done.
$this->_logIn();
// Send HTTP POST request.
curl_setopt($this->_ch, CURLOPT_URL, 'https://www.google.com/voice/call/cancel/');
curl_setopt($this->_ch, CURLOPT_POST, TRUE);
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, array(
'_rnr_se' => $this->_rnr_se,
'forwardingNumber' => '+1'.$from_number,
'outgoingNumber' => $number,
'phoneType' => $types[$phone_type],
'remember' => 0,
'subscriberNumber' => 'undefined'
));
curl_exec($this->_ch);
}
The next suggested addition allows getting the MP3 file of a Google Voice voicemail, assuming you know the id of the voicemail:
/**
* Get MP3 of a Google Voice Voicemail.
*/
public function getVoicemailMP3($message_id) {
// Login to the service if not already done.
$this->_logIn();
// Send HTTP POST request.
curl_setopt($this->_ch, CURLOPT_URL, "https://www.google.com/voice/media/send_voicemail/$message_id/");
curl_setopt($this->_ch, CURLOPT_POST, FALSE);
curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, TRUE);
$results = curl_exec($this->_ch);
return $results;
}
Two suggested additions/revision. The first removes references to Gizmo (RIP) and allows canceling a call in progress (replaces existing callNumber function and adds cancelCall function):
The next suggested addition allows getting the MP3 file of a Google Voice voicemail, assuming you know the id of the voicemail: