Reply
Occasional Contributor
WilliamP6
Posts: 3
Registered: ‎01-25-2012

Error 415 when creating contact for addition to list (PHP):

[ Edited ]

Here is the core function I'm using (this is almost verbatim from the sample code):

 

function doServerCall($request, $parameter = '', $type = 'GET') {
    $ch = curl_init();
    $request = "https://{$GLOBALS['_cc_path']}{$request}";

    curl_setopt($ch, CURLOPT_URL, $request);
    curl_setopt($ch, CURLOPT_USERAGENT,
        'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, $GLOBALS['_cc_login']);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER,
        Array("Content-Type:application/atom+xml", 'Content-Length: ' . 
        strlen($parameter)));
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    switch ($type) {
        case 'POST':                  
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);
            break;
        case 'PUT':
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);
            print "Sending to $request\n\n$parameter\n\n";
            break;
        case 'DELETE':
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            break;
        default:
            curl_setopt($ch, CURLOPT_HTTPGET, 1);
            break;
    }

    $result['response'] = curl_exec($ch);
    $result['error'] = curl_error($ch);

    curl_close($ch);
   
    return $result;
}

This actually works just fine, as far as I can tell, for checking to see if an email exists, getting the detailed info about that contact, and updating the contact to add or remove a list. However, when I try to add a contact, I get a 415 error, and I can't figure out why. As you can see, I added a line to print out the URL it's calling, as well as the XML being passed, if it's a PUT request. Here is the print:

 

Sending to https://api.constantcontact.com/ws/customers/BillPride/contacts

<?xml version="1.0" encoding="UTF-8"?> <entry xmlns="http://www.w3.org/2005/Atom"> <title type="text">Contact: info@fentonwebdesignfirm.com</title> <updated>2012-01-27T18:37:41Z</updated> <author> <name>Homeschool World</name> </author> <id>data:,none</id> <summary type="text">Contact</summary> <content type="application/vnd.ctct+xml"> <Contact xmlns="http://ws.constantcontact.com/ns/1.0/"> <EmailAddress>info@fentonwebdesignfirm.com</EmailAddress> <Name>Theodore Pride</Name> <FirstName>Theodore</FirstName> <LastName>Pride</LastName> <Addr1>1731 Smizer Mill Rd.</Addr1> <Addr2></Addr2> <City>Fenton</City> <StateCode>MO</StateCode> <CountryCode>US</CountryCode> <CountryName>United States</CountryName> <PostalCode>63026</PostalCode> <OptInSource>ACTION_BY_CONTACT</OptInSource> <ContactLists> <ContactList id="http://api.constantcontact.com/ws/customers/BillPride/lists/4" /> </ContactLists> </Contact> </content> </entry>

 And here is what I'm getting back as a result:

 

Array
(
    [response] => 
    [error] => The requested URL returned error: 415
)

What am I doing wrong? I used the basic example XML and just added a few fields, plus if I'm understanding correctly, error 415 is Unsupported Media type, which is triggered by using the wrong send type (GET, PUT, POST) or not including the proper header - both of which don't seem to be happening here and would have nothing to do with the data I'm passing.

http://community.constantcontact.com/t5/Documentation/Creating-a-Contact/ba-p/25059

Ryan_D
Posts: 453
Topics: 0
Kudos: 20
Solutions: 21
Registered: ‎06-28-2011

Re: Error 415 when creating contact for addition to list (PHP):

Hey,

 

I think you may be getting some wrong information passed through your parameters.  I have condensed your code and was able to get a correct POST to my account.

 

<?php
$parameter = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
  <title type="text">Contact: info@fentonwebdesignfirm.com</title>
  <updated>2012-01-27T18:37:41Z</updated>
  <author>
    <name>Homeschool World</name>
  </author>
  <id>data:,none</id>
  <summary type="text">Contact</summary>
  <content type="application/vnd.ctct+xml">
    <Contact xmlns="http://ws.constantcontact.com/ns/1.0/">
      <EmailAddress>info2@fentonwebdesignfirm.com</EmailAddress>
      <Name>Theodore Pride</Name>
      <FirstName>Theodore</FirstName>
      <LastName>Pride</LastName>
      <Addr1>1731 Smizer Mill Rd.</Addr1>
      <Addr2></Addr2>
      <City>Fenton</City>
      <StateCode>MO</StateCode>
      <CountryCode>US</CountryCode>
      <CountryName>United States</CountryName>
      <PostalCode>63026</PostalCode>
      <OptInSource>ACTION_BY_CUSTOMER</OptInSource>
      <ContactLists>
        <ContactList id="http://api.constantcontact.com/ws/customers/BillPride/lists/4" />
      </ContactLists>
    </Contact>
  </content>
</entry>';



$ch = curl_init();
$request = "https://api.constantcontact.com/ws/customers/{username}/contacts";
$auth = "myapikey%username:password";
$type = "POST";

curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_USERAGENT,
		'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $auth);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
		Array("Content-Type:application/atom+xml", 'Content-Length: ' .
				strlen($parameter)));
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

switch ($type) {
	case 'POST':
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);
		break;
	case 'PUT':
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
		curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);
		print "Sending to $request\n\n$parameter\n\n";
		break;
		case 'DELETE':
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
	break;
	default:
	curl_setopt($ch, CURLOPT_HTTPGET, 1);
	break;
}

$result['response'] = curl_exec($ch);
$result['error'] = curl_error($ch);

curl_close($ch);
 

print_r($result);

 I would try that on your environment once you change the $auth value as well as the XML.  It went through with no issues at all.

 

Please let me know if this helps.

 

Ryan Davis
Support Engineer, Constant Contact
Occasional Contributor
WilliamP6
Posts: 3
Registered: ‎01-25-2012

Re: Error 415 when creating contact for addition to list (PHP):

Ok, the problem was I was using PUT instead of POST. But this makes zero sense! Editing a contact's information doesn't work with POST, but does with PUT (I get a response code of 1), while adding a contact doesn't work with PUT but does with POST. Were the modes picked at random or is there some logical reason for this?

Ryan_D
Posts: 453
Topics: 0
Kudos: 20
Solutions: 21
Registered: ‎06-28-2011

Re: Error 415 when creating contact for addition to list (PHP):

Hey.

 

No, the REST service rely's on CRUD. Create (POST), Read(GET), Update(PUT) and Destroy(DELETE).  Our database is structed so that if a contact exists, you caont create it again casue its already there.  Therefore you would have to update its information.  Typically on the create it is always best to do a search to see if the contact exists, and control the outcome of what you want to do with a conditonal statement.  You can find more information about searchinf for a contact here.

 

Please let me know if this helps.  We also have a great PHP wrapper that would save you a lot of time while working with this code.  You can download it here.

Ryan Davis
Support Engineer, Constant Contact
Frequent Visitor
JoyD04
Posts: 1
Registered: ‎05-18-2012

Re: Error 415 when creating contact for addition to list (PHP):

Hello Ryan

 

This simply to say thank you for your code, I had to implemented it for my own purpose and of course I had all the errors number you can expect following the official receipt on the site " Add Contact " But your code works perferctly and save me a lot of times.

 

Thank you very much

Peter