如何解决如何在 php 中创建 Google Alert
我花了很多时间在上面,但没有找到任何可行的解决方案...... 我试过下面的代码..但总是其他情况正在运行“没有找到登录表单1” 我已经尝试了另一个 coders11 实现的 api,但它也被弃用了... 我找到了许多其他解决方案,但在 php 中没有...我正在 php 中寻找解决方案...
class googleAlerts{
public function createAlert($alert){
$USERNAME = 'XXXXXX@gmail.com';
$PASSWORD = 'YYYYYY';
$COOKIEFILE = 'cookies.txt';
$ch = curl_init();
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30);
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_COOKIEJAR,$COOKIEFILE);
curl_setopt($ch,CURLOPT_COOKIEFILE,CURLOPT_HEADER,0);
curl_setopt($ch,120);
curl_setopt($ch,CURLOPT_TIMEOUT,120);
curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage');
$data = curl_exec($ch);
$formFields = $this->getFormFields($data);
$formFields['Email'] = $USERNAME;
$formFields['Passwd'] = $PASSWORD;
unset($formFields['PersistentCookie']);
$post_string = '';
foreach($formFields as $key => $value) {
$post_string .= $key . '=' . urlencode($value) . '&';
}
$post_string = substr($post_string,-1);
curl_setopt($ch,'https://accounts.google.com/ServiceLoginAuth');
curl_setopt($ch,CURLOPT_POST,CURLOPT_POSTFIELDS,$post_string);
$result = curl_exec($ch);
if (strpos($result,'<title>') === false) {
return false;
} else {
curl_setopt($ch,'http://www.google.com/alerts');
curl_setopt($ch,0);
curl_setopt($ch,null);
$result = curl_exec($ch);
curl_setopt($ch,'http://www.google.com/alerts/create');
curl_setopt($ch,0);
$result = curl_exec($ch);
//var_dump($result);
$result = $this->getFormFieldsCreate($result);
$result['q'] = $alert;
$result['t'] = '7';
$result['f'] = '1';
$result['l'] = '0';
$result['e'] = 'feed';
unset($result['PersistentCookie']);
$post_string = '';
foreach($result as $key => $value) {
$post_string .= $key . '=' . urlencode($value) . '&';
}
$post_string = substr($post_string,-1);
curl_setopt($ch,$post_string);
$result = curl_exec($ch);
curl_setopt($ch,'http://www.google.com/alerts/manage');
$result = curl_exec($ch);
if (preg_match_all('%'.$alert.'(?=</a>).*?<a href=[\'"]http://www.google.com/alerts/feeds/([^\'"]+)%i',$result,$matches)) {
return ('http://www.google.com/alerts/feeds/'.$matches[1][0]);
} else {
return false;
}
}
}
private function getFormFields($data)
{
if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is',$data,$matches)) {
$inputs = $this->getInputs($matches[1]);
return $inputs;
} else {
die('didnt find login form');
}
}
private function getFormFieldsCreate($data)
{
if (preg_match('/(<form.*?name=.?.*?<\/form>)/is',$matches)) {
$inputs = $this->getInputs($matches[1]);
return $inputs;
} else {
die('didnt find login form1');
}
}
private function getInputs($form)
{
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is',$form,$matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/\s{2,}/',' ',$matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i',$el,$name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i',$value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}
}
$alert = new googleAlerts;
echo $alert->createAlert('YOUR ALERT');```
解决方法
您不能再使用密码和电子邮件登录谷歌警报,您必须通过登录谷歌警报并将它们从开发控制台复制出来,然后在执行 curl 请求时将它们作为参数传递来预先创建 cookie。查看我用 php 编写的 google alerts api。也许这对你有帮助https://github.com/Trivo25/google-alerts-api-php
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。