我们已实施了一项功能,可以发布到Google+信息流。
问题在于,它在Google+中提供了“可疑的登录尝试”消息,而几天前它的工作正常。
请在下面找到示例代码:
class GooglePlus_StatusUpdate {
protected $_email;
protected $_password;
protected $_pageid = false;
protected $_debug = false;
public $status;
protected $_api_key = 'AIzaSyCtbzna9Ar8DRSA01DgaFQaUb687J2wOPY';
protected $_user_id;
protected $path;
protected $_interval = 90; //seconds
private $__cookies = 'cookie.txt';
private $__sleeptime = 1;
private $__uagent = 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)';
private $__pc_uagent = 'Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1';
function __construct($options) {
if(!empty($options)) {
foreach($options as $key =>$value) {
$methodName = 'set'.ucFirst($key);
if(method_exists($this,$methodName)) {
$this->$methodName($value);
}
}
}
}
public function setUser_id($val) {
if(null == $this->_user_id) {
$this->_user_id = $val;
}
}
public function setEmail($val) {
if(null == $this->_email) {
$this->_email = $val;
}
}
public function setPassword($val) {
if(null == $this->_password) {
$this->_password = $val;
}
}
public function setStatus($val) {
if(null == $this->status) {
$this->status = $val;
}
}
public function postStatus($status=null) {
if($status) {
$this->status = $status;
}
$this->path = APPLICATION_PATH . "/public/";
@unlink($this->path.$this->__cookies); //delete previous cookie file if exists
touch($this->path . "/public/".$this->__cookies);
$res = $this->login_data();
$this->login($res);
sleep($this->__sleeptime);
$status = $this->update_profile_status();
sleep($this->__sleeptime);
$this->logout();
return $status;
}
private function tidy($str) {
return rtrim($str, "&");
}
private function login_data() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->path.$this->__cookies);
curl_setopt($ch, CURLOPT_USERAGENT, $this->__uagent);
curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/ServiceLoginAuth?service=oz");
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->__cookies);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$buf = utf8_decode(html_entity_decode(curl_exec($ch)));
$buf = str_replace( '&', '&', $buf ); // just in case any correctly encoded
$buf = str_replace( '&', '&', $buf ); // now encode them all again
curl_close($ch);
if($this->_debug) {
echo "\n[+] Sending GET request to: https://plus.google.com/\n\n";
echo "\n[+] username is " . $this->_email . "/\n\n";
}
$toreturn = '';
$doc = new DOMDocument;
if($this->_debug) {
echo $buf;
}
$doc->loadhtml($buf);
$inputs = $doc->getElementsByTagName('input');
foreach ($inputs as $input) {
switch ($input->getAttribute('name')) {
case 'Email': {
$toreturn .= 'Email=' . urlencode($this->_email) . '&';
break;
}
case 'Passwd': {
$toreturn .= 'Passwd=' . urlencode($this->_password) . '&';
break;
}
default: {
$toreturn .= $input->getAttribute('name') . '=' . urlencode($input->getAttribute('value')) . '&';
}
}
}
return array($this->tidy($toreturn), $doc->getElementsByTagName('form')->item(0)->getAttribute('action'));
}
private function login($postdata) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->path.$this->__cookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->path.$this->__cookies);
curl_setopt($ch, CURLOPT_USERAGENT, $this->__uagent);
curl_setopt($ch, CURLOPT_URL, $postdata[1]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata[0]);
$buf = curl_exec($ch); #this is not the g+ home page, because the b**** doesn't redirect properly
curl_close($ch);
if ($this->_debug) {
echo $buf;
echo "\n[+] Sending POST request to: " . $postdata[1] . "\n\n";
}
}
private function update_profile_status() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->path.$this->__cookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->path.$this->__cookies);
curl_setopt($ch, CURLOPT_USERAGENT, $this->__uagent);
curl_setopt($ch, CURLOPT_URL, 'https://m.google.com/app/plus/?v=compose&group=m1c&hideloc=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$buf = utf8_decode(html_entity_decode(str_replace('&', '', curl_exec($ch))));
$header = curl_getinfo($ch);
curl_close($ch);
if ($this->_debug) {
echo $buf;
}
$params = '';
$doc = new DOMDocument;
$doc->loadxml($buf);
$inputs = $doc->getElementsByTagName('input');
foreach ($inputs as $input) {
if (($input->getAttribute('name') != 'editcircles')) {
$params .= $input->getAttribute('name') . '=' . urlencode($input->getAttribute('value')) . '&';
}
}
$params .= 'newcontent=' . urlencode($this->status);
//$baseurl = $doc->getElementsByTagName('base')->item(0)->getAttribute('href');
$baseurl = 'https://m.google.com' . parse_url($header['url'], PHP_URL_PATH);
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->path.$this->__cookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->path.$this->__cookies);
curl_setopt($ch, CURLOPT_USERAGENT, $this->__uagent);
//delete group=b0& in the line below, to post just to your circles, not to public
curl_setopt($ch, CURLOPT_URL, $baseurl . '?v=compose&group=m1c&group=b0&hideloc=1&a=post');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $baseurl . '?v=compose&group=m1c&group=b0&hideloc=1');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$buf = curl_exec($ch);
$header = curl_getinfo($ch);
curl_close($ch);
if ($this->_debug) {
echo "<pre>";
echo "here----".print_r($header).$buf;
echo "\n[+] POST Updating status on: " . $baseurl . "\n\n";
}
$info = $this->getStatusInfo(time(),$this->_interval); //(seconds) seems to be a suitable interval
if ($this->_debug) {
echo "<pre>";
print_r($info);
}
return $info;
}
private function getStatusInfo($posted_time,$interval) {
if ($this->_debug) {
echo "posted_time:".date('Y-m-d H:i:s',$posted_time);
}
$ch = curl_init();
$url = "https://www.googleapis.com/plus/v1/people/{$this->_user_id}/activities/public?key=".$this->_api_key;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 0);
$res = curl_exec($ch);
$res = json_decode($res);
$ret = array();
$ret['status'] = false;
if(!empty($res->items)) {
$activity = $res->items[0];
if($this->_debug) {
echo "<pre>";
print_r($activity);
}
$published_time = strtotime($activity->published);
if ($this->_debug) {
echo "publiched_time:".date('Y-m-d H:i:s',$published_time);
}
if($published_time > $posted_time) {
$time_diff = (int)($published_time - $posted_time);
}else {
$time_diff = (int)($posted_time - $published_time);
}
if($time_diff <= $interval) {
//this is the last post
$ret = array();
$ret['status'] = true;
$ret['post_id'] = $activity->id;
return $ret;
}
}
return $ret;
}
private function logout() {
if ($this->_debug) {
echo "\n[+] GET Logging out: \n\n";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->path.$this->__cookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->path.$this->__cookies);
curl_setopt($ch, CURLOPT_USERAGENT, $this->__uagent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/m/logout');
$buf = curl_exec($ch);
curl_close($ch);
if ($this->_debug) {
echo $buf;
}
}
}