How to use Constant Contact API? - php

I want to use API of the constant contact and want to insert user email using PHP while user register to the site.
please reply if any help.
Thanks in advance.

// fill in your Constant Contact login and API key
$ccuser = 'USERNAME_HERE';
$ccpass = 'PASS_HERE';
$cckey = 'APIKEY_HERE';
// fill in these values
$firstName = "";
$lastName = "";
$emailAddr = "";
$zip = "";
// represents the contact list identification number(s)
$contactListId = INTEGER_OR_ARRAY_OF_INTEGERS_HERE;
$contactListId = (!is_array($contactListId)) ? array($contactListId) : $contactListId;
$post = new SimpleXMLElement('<entry></entry>');
$post->addAttribute('xmlns', 'http://www.w3.org/2005/Atom');
$title = $post->addChild('title', "");
$title->addAttribute('type', 'text');
$post->addChild('updated', date('c'));
$post->addChild('author', "");
$post->addChild('id', 'data:,none');
$summary = $post->addChild('summary', 'Contact');
$summary->addAttribute('type', 'text');
$content = $post->addChild('content');
$content->addAttribute('type', 'application/vnd.ctct+xml');
$contact = $content->addChild('Contact');
$contact->addAttribute('xmlns', 'http://ws.constantcontact.com/ns/1.0/');
$contact->addChild('EmailAddress', $emailAddr);
$contact->addChild('FirstName', $firstName);
$contact->addChild('LastName', $lastName);
$contact->addChild('PostalCode', $zip);
$contact->addChild('OptInSource', 'ACTION_BY_CUSTOMER');
$contactlists = $contact->addChild('ContactLists');
// loop through each of the defined contact lists
foreach($contactListId AS $listId) {
$contactlist = $contactlists->addChild('ContactList');
$contactlist->addAttribute('id', 'http://api.constantcontact.com/ws/customers/' . $ccuser . '/lists/' . $listId);
}
$posturl = "https://api.constantcontact.com/ws/customers/{$ccuser}/contacts";
$authstr = $cckey . '%' . $ccuser . ':' . $ccpass;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $authstr);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post->asXML());
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/atom+xml"));
curl_setopt($ch, CURLOPT_HEADER, false); // Do not return headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // If you set this to 0, it will take you to a page with the http response
$response = curl_exec($ch);
curl_close($ch);
// returns true on success, false on error
return (!is_numeric($response));

The developers of ConstantContact have launched PHP library to handle such kind of tasks.
Following version is for older PHP versions (5.2 or lesser). Download code from here: https://github.com/constantcontact/ctct_php_library
Following is an example to add a subscriber's email in your constant contact lists.
After downloading the above library, use following code to add an email in Constant Contact list(s).
session_start();
include_once('ConstantContact.php'); // Set path accordingly
$username = 'YOUR_CONSTANT_CONTACT_USER_NAME';
$apiKey = 'YOUR_API_KEY';
$password = 'YOUR_CONSTANT_CONTACT_PASSWORD';
$ConstantContact = new Constantcontact("basic", $apiKey, $username, $password);
$emailAddress = "new_email#test.com";
// Search for our new Email address
$search = $ConstantContact->searchContactsByEmail($emailAddress);
// If the search did not return a contact object
if($search == false){
$Contact = new Contact();
$Contact->emailAddress = $emailAddress;
//$Contact->firstName = $firstName;
//$Contact->lastName = $lastName;
// represents the contact list identification link(s)
$contactList = LINK_OR_ARRAY_OF_LINKS_HERE;
// For example,
// "http://api.constantcontact.com/ws/customers/USER_NAME/lists/14";
$Contact->lists = (!is_array($contactList)) ? array($contactList) : $contactList;
$NewContact = $ConstantContact->addContact($Contact);
if($NewContact){
echo "Contact Added. This is your newly created contact's information<br /><pre>";
print_r($NewContact);
echo "</pre>";
}
} else {
echo "Contact already exist.";
}
Note: The latest version can be found on the GitHub links given in following URL: http://developer.constantcontact.com/libraries/sample-code.html
But I am not sure if the above example code works with the newer library or not.

If you can't get this to work, you might have to add this curl_setopt:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

Related

Is it possible execute a function in Prestashop via hooks without creating a module?

I have created this test function (lets call it ABtest.php) to send a test message:
$host = "http://localhost:8080";
$id = "123";
$email = "mine.mail#gmail.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "mine.mail:mypassword");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPAUTH, TRUE);
function send($host,$id,$email,$ch){
curl_setopt($ch, CURLOPT_URL, $host . "/c/" . $id . "/your ID/");
curl_setopt($ch, CURLOPT_POSTFIELDS, "to=" . $email . "&subject=Your ID ");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$output = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200 && curl_getinfo($ch, CURLINFO_HTTP_CODE) != 201) {
printf("An error occured (HTTP %d): %sn", curl_getinfo($ch, CURLINFO_HTTP_CODE), $output);
} else {
printf("Success");
}
}
I would like to place this code into my hook hookValidateOrder($params) like this:
public function hookValidateOrder($params)
{
$id = $params['cart']->id;
$host = "http://localhost:8080";
$email = "mine.mail#gmail.com";
// + the rest of my PHP function - see the 1st code block above
}
The problem is that I donĀ“t really know where to place my code. As You can see I am not creating a module, I just want to execute my PHP function via hookValidateOrder($params) hook.
Is that possible and where do I place it?
I don't think so. Not in the proper way.
But you can create an override, in the overrides folder for PaymentModule.php:
abstract class PaymentModule extends PaymentModuleCore
{
public function validateOrder($id_cart, $id_order_state, $amount_paid, $payment_method = 'Unknown', $message = null, $extra_vars = array(), $currency_special = null, $dont_touch_amount = false, $secure_key = false, Shop $shop = null)
{
// do what you need before
parent::validateOrder($id_cart, $id_order_state, $amount_paid, $payment_method, $message, $extra_vars, $currency_special, $dont_touch_amount, $secure_key, $shop);
// do what you need after
}
}
You can also override the any module to do it, or the hook exec and check if it's trying to execute the hookValidateOrder then you place your code.
If you do create an override, don't forget to delete (or rename) the file cache/class_index.php after, to index you new override.

Using cPanel API to create mySQL database and user in PHP

I am trying to create a mysql user and assign it to the created database.
I have tried setting $db_host as IP address, FQD, localhost (since I am running from the same server) etc. All of these has no success.
Any advice on what I'm doing wrong? (xmlapi.php is being incuded fine)
include("xmlapi.php");
$db_host = "usingfullyqualifieddomain";
$cpuser = "myuser";
$cppass = "mypass";
$xmlapi = new xmlapi($db_host);
$xmlapi->set_port(2083);
$xmlapi->password_auth($cpuser,$cppass);
$xmlapi->set_debug(1);
//create database
print $xmlapi->api1_query($cpuser, "Mysql", "adddb", 'myDatabaseName');
//create user
print $xmlapi->api1_query($cpuser, "Mysql", "adduser", array('user' => 'myDBUser','pass'=>'myDBPwd'));
The error is probably related to the way you're making your first service call, to create the database. Here is how I do it, on CPanel 11:
$auth_user = 'XXXXXX';
$auth_pass = 'XXXXX';
$server = 'XXXXXXXX.com';
$json_client = new \xmlapi($server);
$json_client->set_output('json');
$json_client->set_port(2083);
$json_client->password_auth($auth_user, $auth_pass);
$json_client->set_debug(1);
# Create Database
$result = $json_client->api1_query( $auth_user, 'Mysql', 'adddb', array($shop->alias));
var_dump($result);
Note that the fourth parameter should be an array with the parameters, and not a string as you are doing it.
Also notice that the print on the api call result is not very useful for debugging. Try var_dump'ing it, as it will show you some more interesting information to work on.
currently, cpanel support the cpanel json api..
here you can use this code ..
this worked for me well
<?php
$cpanelusername = "cpanelusername";
$cpanelpassword = "cpanelpassword";
$domain = 'mydomain.com';
$query = "https://$domain:2083/json-api/cpanel?cpanel_jsonapi_module=Mysql&cpanel_jsonapi_func=adddb&cpanel_jsonapi_apiversion=1&arg-0=DBNAME";
$curl = curl_init(); // Create Curl Object
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0); // Allow self-signed certs
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0); // Allow certs that do not match the hostname
curl_setopt($curl, CURLOPT_HEADER,0); // Do not include header in output
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); // Return contents of transfer on curl_exec
$header[0] = "Authorization: Basic " . base64_encode($cpanelusername.":".$cpanelpassword) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // set the username and password
curl_setopt($curl, CURLOPT_URL, $query); // execute the query
$result = curl_exec($curl);
if ($result == false) {
error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
// log error if curl exec fails
}
curl_close($curl);
print $result;
?>

Create Google Contact --> Why Only "NAME" not inserted in new contact?

Here is what I have made request :-
// 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();
and my curl POST data code is:-
$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);
Here $contact_detail is nothing but the XML generated above.
And it create new contact successfully, but it can't add NAME of the contact.
Please suggest me changes.
Try to set 'GData-Version:3.0' in your Authorized request Headers, or specify the current API version in you request URL (?v=3), this may helps.

execute external page while doing some function with php?

hello im trying to create a simple script which will create an account with my dedicated server using WHM
here is the simple code
<?php
// your WHM username
$whm_user = 'root';
// your WHM password
$whm_pass = 'pass';
// your WHM hostname
$whm_host = '10.10.10.10';
// new account domain or subdomain name
$user_domain = 'example.com';
// new account username (8 characters or less)
$user_name = 'example';
// new account password
$user_pass = 'example1245';
// new account contact email
$user_email = 'user#example.net';
// new account plan (must be an existing WHM plan)
$user_plan = 'Gold';
// create the account
$site = "https://{$whm_user}:{$whm_pass}#{$whm_host}:2087/scripts/wwwacct";
$params = "?plan={$user_plan}";
$params .= "&domain={$user_domain}";
$params .= "&username={$user_name}";
$params .= "&password={$user_pass}";
$params .= "&contactemail={$user_email}";
$url = $site.$params;
file_get_contents($url);
?>
this return an error
[function.file-get-contents]: failed to open stream: No error in C:\AppServ\www\cp\create-whm-account.php on line 35
then i tried to use CURL
<?php
// your WHM username
$whm_user = 'root';
// your WHM password
$whm_pass = 'pass';
// your WHM hostname
$whm_host = '10.10.10.10';
// new account domain or subdomain name
$user_domain = 'example.com';
// new account username (8 characters or less)
$user_name = 'example';
// new account password
$user_pass = 'example1245';
// new account contact email
$user_email = 'user#example.net';
// new account plan (must be an existing WHM plan)
$user_plan = 'Gold';
// create the account
$site = "https://{$whm_user}:{$whm_pass}#{$whm_host}:2087/scripts/wwwacct";
$params = "?plan={$user_plan}";
$params .= "&domain={$user_domain}";
$params .= "&username={$user_name}";
$params .= "&password={$user_pass}";
$params .= "&contactemail={$user_email}";
$url = $site.$params;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
?>
it doesn't return any error and doesn't work neither
i don't need to return any output as this will work within another functions so i don't need to show any result just create the account within the background
I noticed that you are requesting a HTTPS URL. Extra CURL flags might be required. Very likely:
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
See also: PHP: Curl to fetch html page from HTTPS

bbPress RTX integration

Im currently working on a RTX/Janrain integration with bbPress, but im stuck with a SQL query which doesnt give me any results even though I've been trying with wildcards and an e-mail adress i know is registered.
Sign In
$rpxApiKey = 'xxxxx';
if(isset($_POST['token'])) { /*
STEP 1: Extract token POST parameter
*/ $token = $_POST['token'];
/* STEP 2: Use the token to make the
auth_info API call */ $post_data =
array('token' => $_POST['token'],
'apiKey' => $rpxApiKey,
'format' => 'json');
$curl = curl_init();
curl_setopt($curl,
CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL,
'https://rpxnow.com/api/v2/auth_info');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_HEADER,
false); curl_setopt($curl,
CURLOPT_SSL_VERIFYPEER, false);
$raw_json = curl_exec($curl);
curl_close($curl);
/* STEP 3: Parse the JSON auth_info
response */ $auth_info =
json_decode($raw_json, true);
if ($auth_info['stat'] == 'ok') {
/* STEP 3 Continued: Extract the 'identifier' from the response */
$profile = $auth_info['profile'];
$identifier = $profile['identifier'];
$profile['identifier'];
if (isset($profile['photo'])) {
$photo_url = $profile['photo'];
}
if (isset($profile['displayName'])) {
$name = $profile['displayName'];
}
if (isset($profile['email'])) {
$email = $profile['email'];
}
/* Step 5, Check if user existis in database, if so login, if
not create new user then login*/
global $bbdb; $querystr = "
SELECT * FROM $bbdb->bb_users
WHERE user_email = $email LIMIT
1"; $rtx_user_id =
$bbdb->get_results($querystr, OBJECT);
print_r($rtx_user_id);
if ($rtx_user_id) {
echo "Great success";
wp_set_auth_cookie( (int) $rtx_user_id, 0 ); // 0 = don't
remember, short login, todo: use form
value do_action('bb_user_login',
(int) $rtx_user_id ); } if
(!$rtx_user_id) { echo "Not great
success";}
/* STEP 6: Use the identifier as the unique key to sign the user into
your system.
This will depend on your website implementation, and you should
add your own
code here.
*/
/* an error occurred */ }
else { // gracefully handle the
error. Hook this into your native
error handling system. echo 'An
error occured: ' .
$auth_info['err']['msg']; } } } ?>
The problem accrues in Step 5 which is to check if the user exists.
Thanks in advance,
Marten
As we talked on twitter, the query line should be
$querystr = "SELECT * FROM $bbdb->users WHERE user_email = '$email' LIMIT 1";

Categories