编程语言
首页 > 编程语言> > php – 创建Google联系人 – >为什么只有“NAME”未插入新联系人?

php – 创建Google联系人 – >为什么只有“NAME”未插入新联系人?

作者:互联网

以下是我提出的要求: –

    // create new entry
$doc = new DOMDocument();
$doc->formatOutput = true;

$entry = $doc->createElement('atom:entry');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:atom', 'http://www.w3.org/2005/Atom');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gd', 'http://schemas.google.com/g/2005');
$doc->appendChild($entry);

$cat = $doc->createElement('atom:category');
$cat->setAttribute('scheme', 'http://schemas.google.com/g/2005#kind');
$cat->setAttribute('term', 'http://schemas.google.com/contact/2008#contact');
$entry->appendChild($cat);

// add name element
$name = $doc->createElement('gd:name');
$entry->appendChild($name);
$givenName = $doc->createElement('gd:givenName', $_POST['fname']);
$familyName = $doc->createElement('gd:familyName', $_POST['lname']);
$fullName = $doc->createElement('gd:fullName', $_POST['fname'] . $_POST['lname']);
$name->appendChild($givenName);
$name->appendChild($familyName);
$name->appendChild($fullName);

$content = $doc->createElement('atom:content', 'Notes');
$content->setAttribute('type', 'text');
$entry->appendChild($content);
// add email element
$email = $doc->createElement('gd:email');
$entry->appendChild($email);
$email->setAttribute('address', $_POST['email_id']);
$email->setAttribute('displayName', $_POST['fname']);
$email->setAttribute('primary', 'true');
$email->setAttribute('rel', 'http://schemas.google.com/g/2005#work');

// add im element
$im = $doc->createElement('gd:im');
$entry->appendChild($im);
$im->setAttribute('address', $_POST['email_id']);
$im->setAttribute('protocol', 'http://schemas.google.com/g/2005#GOOGLE_TALK');
$im->setAttribute('primary', 'true');
$im->setAttribute('rel', 'http://schemas.google.com/g/2005#home');
// add phone element
$ph = $doc->createElement('gd:phoneNumber', $_POST['phone']);
$entry->appendChild($ph);
$ph->setAttribute('rel', 'http://schemas.google.com/g/2005#home');

//insert Address
$address = $doc->createElement('gd:structuredPostalAddress');
$address->setAttribute('primary', 'true');
$address->setAttribute('rel', 'http://schemas.google.com/g/2005#work');
$entry->appendChild($address);
$postal = $doc->createElement('gd:postcode', $_POST['postal']);
$country = $doc->createElement('gd:country', $_POST['country']);
$fulladd = $doc->createElement('gd:formattedAddress', $_POST['address']);
$address->appendChild($postal);
$address->appendChild($country);
$address->appendChild($fulladd);

return $doc->saveXML();

和我的卷曲POST数据代码是: –

$url = 'https://www.google.com/m8/feeds/contacts/default/full?oauth_token=' . $accesstoken;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 5);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $contact_detail);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

        $curlheader[0] = "Content-Type: application/atom+xml";
        $curlheader[2] = "Content-length:" . strlen($contact_detail);
        $curlheader[1] = "Content-Transfer-Encoding: binary";
        curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);

        $xmlresponse = curl_exec($curl);

这里$contact_detail只不过是上面生成的XML.
它成功创建新联系人,但无法添加联系人的NAME.

请建议我改变.

解决方法:

尝试在授权请求标头中设置“GData-Version:3.0”,或在请求URL(?v = 3)中指定当前API版本,这可能会有所帮助.

标签:php,google-api,oauth,oauth-2-0,google-api-client
来源: https://codeday.me/bug/20190529/1181088.html