Solved

Error  400 when creating a contact with google api

Posted on 2011-02-22
5
338 Views
Last Modified: 2013-11-30
This code returns 400 error  Response contains no content type
Any ideas?

function create($url, $token)
{

 $contactAtom = new DOMDocument();
  $contactAtom->formatOutput = true;  $topLevel = $contactAtom->createElement('atom:entry');
    $topLevel->setAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom');
    $topLevel->setAttribute('xmlns:gd', 'http://schemas.google.com/g/2005');
  $contactAtom->appendChild($topLevel);  
  $category =  $contactAtom->createElement('atom:category');
    $category->setAttribute('scheme', 'http://schemas.google.com/g/2005#kind');
    $category->setAttribute('term', 'http://schemas.google.com/contact/2008#contact');
  $topLevel->appendChild($category);
  $name = $contactAtom->createElement('gd:name');
    $givenName = $contactAtom->createElement('gd:givenName', "Elizabeth");
    $familyName = $contactAtom->createElement('gd:familyName', "Bennett");
    $fullName = $contactAtom->createElement('gd:fullName', "Elizabeth Bennett");
    $name->appendChild($givenName);
    $name->appendChild($familyName);
    $name->appendChild($fullName);
  $topLevel->appendChild($name);  
 
print $contactAtom->saveXML();

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $contactAtom);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      $curlheader[0] = sprintf("Authorization: AuthSub token=\"%s\"/n   GData-Version:\"3.0\" ", $token);
      $curlheader[1] = 'Content-Type: application/atom+xml';
      curl_setopt($ch, CURLOPT_HTTPHEADER, $curlheader);
      curl_setopt($ch, CURLOPT_HEADER, 1);


      $output = curl_exec($ch);
      curl_close($ch);
      return $output;
}

$sXML =create('https://www.google.com/m8/feeds/contacts/default/full',$tokd);
print_r ($sXML);
0
Comment
Question by:ercantunc
5 Comments
 
LVL 27

Accepted Solution

by:
BigRat earned 500 total points
ID: 34971568
>>curl_setopt($ch, CURLOPT_POSTFIELDS, $contactAtom);

You are not posting fields but an XML data stream.

This code snippett should help

http://snippets.dzone.com/posts/show/2596

0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 39686711
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question