我需要能够以编程方式设置电子邮件过滤器(gmail API 不支持的东西),所以......
我今天注册了 Google Apps for Work 30 天试用版,我在这里阅读了文档,发现了这个PHP 代码(根据我的需要进行了调整,代码在运行 PHP 版本 5.4.30 + CentOS 6.4 的专用机器上运行):
<?php
$user_email = 'super-admin@my-domain.com';
$user_passwd = 'admin-password';
$email_label = 'my-label';
// ================================================================== //
function getEmailXML($is_label)
{
global $user_email, $email_label;
list($username, $domain) = split('@', $user_email);
$result = "<?xml version='1.0' encoding='utf-8'?>" . "\n";
if ($is_label)
{
$result .= "<atom:entry xmlns:atom='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>";
$result .= "<apps:property name=\"label\" value=\"$email_label\" />";
}
else
{
$result .= "<atom:entry xmlns:atom='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>";
$result .= " <apps:property name='to' value='$username+$email_label@$domain' />";
$result .= " <apps:property name='label' value='$email_label' />";
$result .= " <apps:property name='neverSpam' value='true' />";
}
$result .= "</atom:entry>";
return $result;
}
// ================================================================== //
function getAuthToken($account)
{
// Getting the google authentication token
$tk_ch = curl_init();
curl_setopt($tk_ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($tk_ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($tk_ch, CURLOPT_POST, true);
curl_setopt($tk_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($tk_ch, CURLOPT_POSTFIELDS, $account);
curl_setopt($tk_ch, CURLOPT_VERBOSE, true);
$response = curl_exec($tk_ch);
curl_close($tk_ch);
$str_split = split('=', $response);
$token = $str_split[3];
return $token;
}
// ================================================================== //
function email_New_Filter_Or_Label($token, $action)
{
global $user_email;
list($username, $domain) = split('@', $user_email);
$ch = curl_init();
$url_feed = "https://apps-apis.google.com/a/feeds/emailsettings/2.0/$domain/$username/$action";
curl_setopt($ch, CURLOPT_URL, $url_feed);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: GoogleLogin auth="' . trim($token) . '"',
'Content-type: application/atom+xml'
));
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, getEmailXML(($action == 'label'))); // getEmailXML($is_label)
$result = curl_exec($ch);
curl_close($ch);
if ($result) {
return true;
}
return false;
}
// ================================================================== //
$my_acc = array(
'accountType' => 'GOOGLE',
'Email' => $user_email,
'Passwd' => $user_passwd,
'service' => 'apps',
);
$auth_token = getAuthToken($my_acc);
$test = email_New_Filter_Or_Label($auth_token, "label");
// $test = email_New_Filter_Or_Label($auth_token, "filter");
var_dump($test);
?>
登录确实可以正常工作,但创建标签会返回500 错误。php /my-script/path/public_html/my-script.php
通过腻子运行给我这个输出:
* About to connect() to www.google.com port 443 (#0)
* Trying 2a00:1450:4001:805::1012... * connected
* Connected to www.google.com (2a00:1450:4001:805::1012) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* warning: ignoring value of ssl.verifyhost
* skipping SSL peer certificate verification
* SSL connection using SSL_RSA_WITH_RC4_128_SHA
* Server certificate:
* subject: CN=www.google.com,O=Google Inc,L=Mountain View,ST=California,C=US
* start date: Sep 10 13:49:36 2014 GMT
* expire date: Dec 09 00:00:00 2014 GMT
* common name: www.google.com
* issuer: CN=Google Internet Authority G2,O=Google Inc,C=US
> POST /accounts/ClientLogin HTTP/1.1
Host: www.google.com
Accept: */*
Content-Length: 476
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------6f31c3e84eeb
* Done waiting for 100-continue
< HTTP/1.1 200 OK
< Content-Type: text/plain
< X-Frame-Options: DENY
< Cache-control: no-cache, no-store
< Pragma: no-cache
< Expires: Mon, 01-Jan-1990 00:00:00 GMT
< Date: Fri, 19 Sep 2014 23:22:35 GMT
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Content-Length: 1139
< Server: GSE
< Alternate-Protocol: 443:quic,p=0.002
<
* Connection #0 to host www.google.com left intact
* Closing connection #0
* About to connect() to apps-apis.google.com port 443 (#0)
* Trying 2a00:1450:4010:c04::76... * connected
* Connected to apps-apis.google.com (2a00:1450:4010:c04::76) port 443 (#0)
* warning: ignoring value of ssl.verifyhost
* skipping SSL peer certificate verification
* SSL connection using SSL_RSA_WITH_RC4_128_SHA
* Server certificate:
* subject: CN=*.google.com,O=Google Inc,L=Mountain View,ST=California,C=US
* start date: Sep 10 14:03:47 2014 GMT
* expire date: Dec 09 00:00:00 2014 GMT
* common name: *.google.com
* issuer: CN=Google Internet Authority G2,O=Google Inc,C=US
> POST /a/feeds/emailsettings/2.0/my-domain.com/super-admin/label HTTP/1.1
Host: apps-apis.google.com
Accept: */*
Authorization: GoogleLogin auth="DQAAAA8BAABE0TPBcstbG31ry47v9WUzihACSe6j11mMXTMNMiiFN09vtCqbMFr0lv_Y2tPJtavYyJdcI0mGsJLJDuC1_q15q6FgdNbO-mAPfOEg4Q8ybk4V_8CUSQNBFSUByC_CAfbwR2gWMw10wqfo9S8afU0LOfX4PrOAWnpA8Dy-3ZMJi-EkipLZlsehZTuW0WS3zkml1uYHTczYRyyH28Ns27ql8OtCW8KOmxvdgpWGYGAR20Z1kbqj3TUBGwE2j4r37qc25_Lj9sjdczmMuBZT8Fr_8SQSzcQaB_U08_NJcueaY4eUoSJfouxh_OqOjE5OTgrqywIzXDEO5K3kNP3o7egmj4p_p2s3k6ZVl2eowhYVqg"
Content-type: application/atom+xml
Content-Length: 201
* The requested URL returned error: 500 Internal Server Error
* Closing connection #0
bool(false)
我已经花了整个时间来尝试完成这项工作,我搜索了很多(都是徒劳的),有人可以告诉我我的代码有什么问题吗?
提前致谢!