email marketing API - examples

We are presenting examples using PHP and .Net. This is just an overview and original implementation will differ from this example at certain points.

Create Group

php version

<?php

// parse the given URL
$url = parse_url('http://dummyapiserver.com/group/createGroup.aspx');
if ($url['scheme'] != 'http') {
die('Only HTTP request is supported!');
}


// extract host and path:
$host = $url['host'];
$path = $url['path'];


// open a socket connection on port 80
$fp = fsockopen($host, 80);


$data = "<?xml version=‘1.0’ standalone=‘yes’ ?>
<createGroup>
<apiKey>twyo6f6c7ZIjGQgj5hzTPZ2RjqZMDf</apiKey>
<groupName>BS</groupName>
</createGroup>";


// send the request headers:
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ". strlen($data) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);


$result = '';
while(!feof($fp)) {
// receive the result
$result .= fgets($fp, 128);
}


// close the socket connection:
fclose($fp);


// split the result header from the content
$result = explode("\r\n\r\n", $result, 2);


$content = isset($result[1]) ? $result[1] : '';
echo ($content);

?>

C# version

WebRequest req = null;
WebResponse rsp = null;
try
{
StringBuilder data = new StringBuilder(@"<?xml version=""1.0"" standalone=""yes""?>
<createGroup>
<apiKey>twyo6f6c7ZIjGQgj5hzTPZ2RjqZMDf</apiKey>
<groupName>BS</groupName>
</createGroup>");


string uri = "http://dummyapiserver.com/group/createGroup.aspx";
req = WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "text/xml";

// Wrap the request stream with a text-based writer
StreamWriter writer = new StreamWriter(req.GetRequestStream());
// Write the XML text into the stream
writer.WriteLine(data.ToString());
writer.Close();

// Send the data to the webserver
rsp = req.GetResponse();
Stream responseStream = rsp.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string str = reader.ReadToEnd();
Response.Write(str);
}
catch (WebException e)
{

}
catch (Exception ex)
{

}
finally
{
if (req != null) req.GetRequestStream().Close();
if (rsp != null) rsp.GetResponseStream().Close();
}


If you face any difficulties in the implementation process, please contact support@leadsnyou.com.