Pages

Saturday, April 16, 2011

Constant contact integrate using CURL


<?php

//Constant Contact//

$emailaddress = trim("email");

/////////// REGISTER EMAIL WITH CONSTANT CONTACT ///////////////////
$UN = "un";
$PW = "pw";
$Key = "api";
$entry = '<entry xmlns="http://www.w3.org/2005/Atom">
<title type="text"> </title>
<updated>' . date('c') . '</updated>
<author></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>' . $emailaddress . '</EmailAddress>
<OptInSource>ACTION_BY_CUSTOMER</OptInSource>
<ContactLists>
<ContactList id="http://api.constantcontact.com/ws/customers/' . $UN . '/lists/1" />' // Do this for all the lists you want to add to
. '
</ContactLists>
</Contact>
</content>
</entry>';
// Initialize the cURL session
$request ="https://api.constantcontact.com/ws/customers/" . $UN . "/contacts";
$session = curl_init($request);
// Set up digest authentication
$userNamePassword = $Key . '%' . $UN . ':' . $PW ;
// Set cURL options
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_USERPWD, $userNamePassword);
curl_setopt($session, CURLOPT_POST, 1);
curl_setopt($session, CURLOPT_POSTFIELDS , $entry);
curl_setopt($session, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml"));
curl_setopt($session, CURLOPT_HEADER, false); // Do not return headers
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1); // If you set this to 0, it will take you to a page with the http response
// Execute cURL session and close it
$response = curl_exec($session);
curl_close($session);

?>

Saturday, April 9, 2011

Create cookie



<script type="text/javascript">
<!--

/* COOKIES */

var Cookies = {
init: function () {
var allCookies = document.cookie.split('; ');
for (var i=0;i<allCookies.length;i++) {
var cookiePair = allCookies[i].split('=');
this[cookiePair[0]] = cookiePair[1];
}
},
create: function (name,value,hours) {
if (hours) {
var date = new Date();
date.setTime(date.getTime()+(hours*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
this[name] = value;
},
erase: function (name) {
this.create(name,'',-1);
this[name] = undefined;
}
};
Cookies.init();

function saveIt(name) {
var x = document.forms['cookieform'].cookievalue.value;
if (!x)
alert('Please fill in a value in the input box.');
else {
Cookies.create(name,x,1);
alert('Cookie created');
}
}

function readIt(name) {
alert('The value of the cookie is ' + Cookies[name]);
}

function eraseIt(name) {
Cookies.erase(name);
alert('Cookie erased');
}


var x = Cookies['jaycookie'];
if (x) alert('Cookie jaycookie \nthat you set on a previous visit, is still active.\nIts value is ' + x);


// -->
</script>


</head>

<body>

<form name="cookieform" action="#"><p>
The value of the cookie should be <input name="cookievalue" />
</p></form>

<p><a href="javascript:saveIt('jaycookie')" class="page">Create cookie 1</a><br />
<a href="javascript:readIt('jaycookie')" class="page">Read cookie 1</a><br />
<a href="javascript:eraseIt('jaycookie')" class="page">Erase cookie 1</a>.</p>

Monday, April 4, 2011

csv import - Export -- mysql


CSV IMPORT

ini_set('auto_detect_line_endings',1);

$handle = fopen('city.csv', 'r');

while (($data = fgetcsv($handle, 1000, ';')) !== FALSE) {
  
FIELDVALUE1 = str_replace("`","", mysql_real_escape_string($data[0]));
FIELDVALUE2 = str_replace("`","", mysql_real_escape_string($data[1]));

mysql_query("INSERT INTO `TABLE_NAME` (`FIELDNAME1`,`FIELDNAME2`) VALUES ('FIELDVALUE1','FIELDVALUE2')");
 

    $result = (mysql_insert_id()> 0) ? 'Pass' : 'Fail' ;
 
    $output[$result]++;

}

CSV EXPORT



$filename = 'test.csv';
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$out  = "";
$mystr = "";

$out .= '"FIELDNAME1";"FIELDNAME2";"FIELDNAME3";"FIELDNAME4"';
$out .= $csv_terminated;

$sql_query = "select * from TABLE_NAME  where CONDITION";
$sqlFreetrail = mysql_query($sql_query);

while ($user = mysql_fetch_assoc($sqlFreetrail))
{
$FIELDVALUE1 = $user["FIELDNAME1"];
$FIELDVALUE2 = $user["FIELDNAME2"];
$FIELDVALUE3 = $user["FIELDNAME3"];
$FIELDVALUE4 = $user["FIELDNAME4"];


$mystr .= '"'.$FIELDVALUE1.'";"'.$FIELDVALUE2.'";"'.$FIELDVALUE3.'";"'.$FIELDVALUE4.'"';

$mystr .= $csv_terminated;
}

$out  = $out.$mystr;

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;