Quantcast
Viewing all articles
Browse latest Browse all 6358

GoogleVoice PHP Callback script working (Non-Asterisk)

About 4 years ago giqcass from the OBiHai forum with some help from me got a PHP script to initiate a GV callback for outbound calls. Yesterday I checked and the script has been kept current. I now have it working. Here is a link to the script: https://github.com/aaronpk/Google-Voice-PHP-API/blob/master/GoogleVoice.php Below is my modified version. I have the PHP script running under XAMPP web server on a Windows 7 machine. To test the script, just enter the following in a web browser: http://192.168.1.xx/gvcallback.PHP?dialnum=6235941000 Your DID number will ring. After you answer GV will call the number.. 192.168.1.xx is the IP address of the computer running XAMPP Web Server. 6235941000 is the outbound number. I have 2 ways to initiate a call from an ATA. I have a small Tropo PHP script to bridge SIP to HTTP. There is a OBi hack that uses ITSP Provisioning and a small script. Neither is ideal. I tried running the full PHP script under Tropo, but I get a Permission error creating the cookie file. Does anybody know how to fix this error? /No write FilePermission to /tmp/gvcookies.txt Yate supports PHP scripts, but the setup is beyond my knowledge. Would anybody know how to run this script using Yate? Bill Simon? Any better ideas? Both of these would be better than what I setup. The XAMPP setup is fast and easy. Download and install the current XAMPP. Turn on Apache from the XAMPP Control Panel. Create file gvcallback.php using my modified script below and move it to folder XAMPP/htdocs. That's it! Download XAMPP: https://www.apachefriends.org/index.html At the end of the script change Name to your GV name. Enter password. Change 1201xxxxxxx to you DID number (No + sign) If anyone wants to see how I'm initiating calls, just ask. gvcallback.php <?php class GoogleVoice { // Google account credentials. private $_login; private $_pass; // Special string that Google Voice requires in our POST requests. private $_rnr_se; // File handle for PHP-Curl. private $_ch; // The location of our cookies. private $_cookieFile; // Are we logged in already? private $_loggedIn = FALSE; public function __construct($login, $pass) { $this->_login = $login; $this->_pass = $pass; $this->_cookieFile = '/tmp/gvCookies.txt'; $this->_ch = curl_init(); curl_setopt($this->_ch, CURLOPT_COOKIEJAR, $this->_cookieFile); curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($this->_ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"); //was "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" } private function _logIn() { global $conf; if($this->_loggedIn) return TRUE; // Fetch the Google Voice login page input fields $URL='https://accounts.google.com/ServiceLogin?service=grandcentral&passive=1209600&continue=https://www.google.com/voice/b/0/redirection/voice&followup=https://www.google.com/voice/b/0/redirection/voice#inbox'; //adding login to GET prefills with username "&Email=$this->_login" curl_setopt($this->_ch, CURLOPT_URL, $URL); $html = curl_exec($this->_ch); // Send HTTP POST service login request using captured input information. $URL='https://accounts.google.com/signin/challenge/sl/password'; // This is the second page of the two page signin curl_setopt($this->_ch, CURLOPT_URL, $URL); $postarray = $this->dom_get_input_tags($html); // Using DOM keeps the order of the name/value from breaking the code. // Parse the returned webpage for the "GALX" token, needed for POST requests. if((!isset($postarray['GALX']) || $postarray['GALX']=='') && (!isset($postarray['gxf']) || $postarray['gxf']=='')){ $pi1 = var_export($postarray, TRUE); error_log("Could not parse for GALX or gxf token. Inputs from page:\n" . $pi1 . "\n\nHTML from page:" . $html); throw new Exception("Could not parse for GALX or gxf token. Inputs from page:\n" . $pi1); } $postarray['Email'] = $this->_login; //Add login to POST array $postarray['Passwd'] = $this->_pass; //Add password to POST array curl_setopt($this->_ch, CURLOPT_POST, TRUE); curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $postarray); $html = curl_exec($this->_ch); // Test if the service login was successful. $postarray = $this->dom_get_input_tags($html); // Using DOM keeps the order of the name/value from breaking the code. if(isset($postarray['_rnr_se']) && $postarray['_rnr_se']!='') { $this->_rnr_se = $postarray['_rnr_se']; $this->_loggedIn = TRUE; } else { $pi2 = var_export($postarray, TRUE); error_log("Could not log in to Google Voice with username: " . $this->_login . "\n\nMay need to change scraping. Here are the inputs from the page:\n". $pi2 ); //add POST action information from DOM. May help hunt down single or dual sign on page changes. throw new Exception("Could not log in to Google Voice with username: " . $this->_login . "\nLook at error log for detailed input information.\n"); } } /** * 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); } /** * Source from http://www.binarytides.com/php-get-name-and-value-of-all-input-tags-on-a-page-with-domdocument/ * Generic function to fetch all input tags (name and value) on a page * Useful when writing automatic login bots/scrapers */ private function dom_get_input_tags($html) { $post_data = array(); // a new dom object $dom = new DomDocument; //load the html into the object @$dom->loadHTML($html); //@suppresses warnings //discard white space $dom->preserveWhiteSpace = FALSE; //all input tags as a list $input_tags = $dom->getElementsByTagName('input'); //get all rows from the table for ($i = 0; $i < $input_tags->length; $i++) { if( is_object($input_tags->item($i)) ) { $name = $value = ''; $name_o = $input_tags->item($i)->attributes->getNamedItem('name'); if(is_object($name_o)) { $name = $name_o->value; $value_o = $input_tags->item($i)->attributes->getNamedItem('value'); if(is_object($value_o)) { $value = $input_tags->item($i)->attributes->getNamedItem('value')->value; } $post_data[$name] = $value; } } } return $post_data; } } // NOTE: Full email address required. $gv = new GoogleVoice('Name@gmail.com', 'password'); // Call a phone from one of your forwarding phones. $dialnum = $_GET["dialnum"]; $gv->callNumber($dialnum, '1201xxxxxxx', 'home'); ?>

Viewing all articles
Browse latest Browse all 6358

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>