我如何使用PHP将联系人添加到Google Contacts api v3中的组中

问题描述 投票:0回答:1

我尝试将联系人添加到组中。但是它以HTTP-CODE响应:400(错误请求)。怎么了?这是带有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);
google-api google-contacts-api php-curl
1个回答
0
投票

在这里您将找到解决方案: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);

默认将不起作用。您可以not使用此:

<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.");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.