php - 如何使用 PHP 将联系人添加到 google 联系人 api v3 中的组
问题描述
我尝试将联系人添加到组。但它响应 HTTP-CODE: 400 (Bad Request)。出了什么问题?这是带有 400 错误请求错误的代码:
/** config: */
$access_token = "YOUR_ACCESS_TOKEN"; //example: ya29.ImC7B2RO5vkjhsdfdOU0tcn3jI1uP7N5wkxEEEEJF01ZbD4d1LLLLLf5gC06Lh5LJ49FnqHglDjxh5SrdX3jFhOtDQoakSi4XWm5XYHCMhBSiYbFbKqxSvP6_6_rB5_UJLg
$etag = '"YOUR_ETAG."' //example: "SHc8fTVSLLt7I2AAXBFbGEsRTgE."
$contactGroup = "YOUR_CONTACTGROUP"; //example: 1b1bccc40d9e0fff
$contactID = "YOUR_CONTACTS_ID"; //example: 5085d7c29e37dd51
$contactXML = "<entry gd:etag='". $etag ."'>
<id>http://www.google.com/m8/feeds/contacts/default/base/". $contactID ."</id>
<gContact:groupMembershipInfo deleted='false'
href='http://www.google.com/m8/feeds/groups/default/base/". $contactGroup ."'/>
</entry>";
$headers = array(
'Host: www.google.com'
,'Gdata-version: 3.0'
,'Content-length: '.strlen($contactXML)
,'If-Match: '. $etag
,'Content-type: application/atom+xml'
,'Authorization: OAuth '.$access_token
,'X-HTTP-Method-Override: PUT'
);
$contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full/'. $contactID;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $contactQuery );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $contactXML);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$resultat = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo("Resultat=".print_r($resultat,1) ."<br>");
echo("HTTP-CODE: ". $httpcode);
解决方案
在这里你会找到解决方案:https ://vike.io/de/204411/
$contactXML = '<?xml version="1.0" encoding="utf-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:gContact="http://schemas.google.com/contact/2008">
<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
<gd:name>
<gd:givenName>'.utf8_encode(str_replace("&",term_user($benutzerid, " und "), $row4['vorname'])).'</gd:givenName>
<gd:fullName>'.utf8_encode(str_replace("&",term_user($benutzerid, " und "),getNameString($row4['id'], true))).'</gd:fullName>
<gd:familyName>'.utf8_encode(str_replace("&", term_user($benutzerid, " und "), $row4['name'])).'</gd:familyName>
</gd:name>
<atom:content type="text">'.utf8_encode($row4['kommentar']).' (von aBusiness CRM)</atom:content>
<gd:email rel="http://schemas.google.com/g/2005#home" primary="true" address="'.utf8_encode($row4['email']).'"/>
<gd:email rel="http://schemas.google.com/g/2005#home" address="'.utf8_encode($row4['email2']).'"/>
<gd:email rel="http://schemas.google.com/g/2005#home" address="'.utf8_encode($row4['email3']).'"/>
<gd:phoneNumber rel="http://schemas.google.com/g/2005#home" primary="true">'.utf8_encode($row4['telefon']).'</gd:phoneNumber>
<gd:phoneNumber rel="http://schemas.google.com/g/2005#home">'.utf8_encode($row4['telefon2']).'</gd:phoneNumber>
<gd:phoneNumber rel="http://schemas.google.com/g/2005#home">'.utf8_encode($row4['telefon3']).'</gd:phoneNumber>
<gd:phoneNumber rel="http://schemas.google.com/g/2005#home">'.utf8_encode($row4['telefon4']).'</gd:phoneNumber>
<gd:phoneNumber rel="http://schemas.google.com/g/2005#home">'.utf8_encode($row4['telefon5']).'</gd:phoneNumber>
<gd:structuredPostalAddress
rel="http://schemas.google.com/g/2005#work"
primary="true">
<gd:city>'.utf8_encode($row4['ort']).'</gd:city>
<gd:street>'.utf8_encode($row4['adresse']).'</gd:street>
<gd:postcode>'.utf8_encode($row4['plz']).'</gd:postcode>
<gd:country>'.utf8_encode($row4['land']).'</gd:country>
</gd:structuredPostalAddress>
<gContact:groupMembershipInfo deleted="false" href="http://www.google.com/m8/feeds/groups/'.$row['email'].'/base/'. $groupId .'"/>
<gContact:groupMembershipInfo deleted="false" href="http://www.google.com/m8/feeds/groups/'.$row['email'].'/base/6"/>
</atom:entry>';
$headers = array(
'Host: www.google.com'
,'Gdata-version: 3.0'
,'Content-length: '.strlen($contactXML)
,'Content-type: application/atom+xml'
,'Authorization: OAuth '.$access_token
);
$contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $contactQuery );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $contactXML);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$resultat = curl_exec($ch);
默认将不起作用。你不能使用这个:
<gContact:groupMembershipInfo deleted="false" href="http://www.google.com/m8/feeds/groups/default/base/'. $groupId .'"/>
您必须使用来自 oAuth 帐户的电子邮件。您可以像这样获取电子邮件并将其保存在数据库中:
/** Die Account-ID holen */
$contactQuery = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=". urlencode($access_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $contactQuery );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$result = curl_exec($ch);
$result = json_decode($result);
$accountid = $result->id;
if(!$accountid) die("Error");
$sql = "update abuoauth set
accountid = ". dbsecurestring($accountid) ."
,`email` = ". dbsecurestring($result->email) ."
where id = ". dbsecurenumber($abuoauthid) ."
";
mysql_query($sql);
创建联系人组:
/** Kontaktgruppe erstellen */
$contactXML = '<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:gd="http://schemas.google.com/g/2005">
<category scheme="http://schemas.google.com/g/2005#kind"
term="http://schemas.google.com/g/2008#group"/>
<title>aBusiness</title>
<gd:extendedProperty name="more info">
<description>aBusiness Kontakte</description>
</gd:extendedProperty>
</entry>';
$headers = array(
'Host: www.google.com'
,'Gdata-version: 3.0'
,'Content-length: '.strlen($contactXML)
,'Content-type: application/atom+xml'
,'Authorization: OAuth '.$access_token
);
$contactQuery = 'https://www.google.com/m8/feeds/groups/default/full';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $contactQuery );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $contactXML);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$resultat = curl_exec($ch);
要检索联系人组:
/** Kontaktgruppe "aBusiness schnappen" */
$groupId = "";
$headers = array(
'Host: www.google.com'
,'Gdata-version: 3.0'
,'Content-type: application/atom+xml'
,'Authorization: OAuth '.$access_token
);
$contactQuery = 'https://www.google.com/m8/feeds/groups/default/full';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $contactQuery );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$resultat = curl_exec($ch);
$posTitle = strpos($resultat, "<description>aBusiness Kontakte</description>");
if($posTitle !== false) {
/** Kontatkgruppe parsern */
$iPosLast = 0;
$iPos = strpos($resultat, "/base/");
while($iPos !== false) {
$iPosLast = $iPos;
$iPos = strpos($resultat, "/base/", $iPos+1);
if($iPos > $posTitle) {
/** Eine Position zu weit. */
$iPos = $iPosLast;
break;
}
}
/** Anfang zuschneiden */
$strCut = substr($resultat, $iPosLast+6);
$groupId = getPartOfString($strCut, "", "</id>");
}
删除联系人:
$headers = array(
'Host: www.google.com'
,'Gdata-version: 3.0'
,'Content-type: application/atom+xml'
,'Authorization: OAuth '.$access_token
,'If-Match: '. $row3['etag']
,'X-HTTP-Method-Override: DELETE'
);
if($row3['googleid']) {
$contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full/'. $row3['googleid'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $contactQuery );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$resultat = curl_exec($ch);
if(strpos($resultat, "200 OK") !== false) {
mysql_query("delete from googlecontacts where id = ". dbsecurenumber($row3['id']));
alert("Google Kontakt mit Kunde-ID ". $row3['kundeid'] ." gelöscht.");
}
}
推荐阅读
- terminal - 字符显示宽度的终端控制序列?
- listview - 如何删除 Flutter ListView 中的剪辑?
- r - R:如何将相同的变量(行号)添加到列表中的每个数据框?
- go - “函数体外的非声明语句”错误golang
- python - 将 ListA 元素与部分匹配的 ListB 元素连接起来
- node.js - MongoError:E11000 重复键错误集合:timeformationBD.formations 索引:description_1 重复键:{:null}
- c# - Intent ActionCall 没有以 xamarin 形式拨打电话
- node.js - Firebase Cloud Functions - 无法读取未定义的属性“forEach”
- html - 在 Angular 2/4 中实现自定义下拉菜单
- php - 使用 Carbon 在 laravel 中将日期转换为毫秒