Last active
December 12, 2015 08:09
-
-
Save RobertRM/4742177 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Function to pull data needed from PB's server | |
function get_web_page($url) { | |
// Set up the options that cURL will use. | |
$options = array( | |
CURLOPT_HTTPHEADER => array('ApiKey:5393c0a3-2ff2-48f3-a450-288cb0205a61'), // Add API Key to the header for the request | |
CURLOPT_RETURNTRANSFER => true, // return web page | |
CURLOPT_HEADER => false, // don't return headers | |
); | |
// Start cURL | |
$ch = curl_init( $url ); | |
// Set the options | |
curl_setopt_array( $ch, $options ); | |
// Execute the request | |
$content = curl_exec( $ch ); | |
// Close the request | |
curl_close( $ch ); | |
// Return the comment | |
return $content; | |
} | |
//Function to send updated data to PB's database | |
function send_data($url,$json) { | |
// Set up the options that cURL will use. | |
$options = array( | |
CURLOPT_HTTPHEADER => array('ApiKey:5393c0a3-2ff2-48f3-a450-288cb0205a61', "Content-type:application/json"), // Add API Key to the header for the request | |
CURLOPT_CUSTOMREQUEST => "PUT", // erases data from HTML | |
//CURLOPT_PUT => true, | |
CURLOPT_POSTFIELDS => $json, // information that will be posted | |
CURLOPT_RETURNTRANSFER => true, // return web page | |
); | |
// Start cURL. $ch is the cURL session. | |
$ch = curl_init($url); | |
// Set the options. this needs the cURL session name too. | |
curl_setopt_array($ch, $options); | |
// Execute the request | |
$content = curl_exec($ch); | |
// Close the request | |
curl_close($ch); | |
// Return the comment from PB | |
return $content; | |
} | |
// Take the returned content from the get_web_page function and parse it with XML | |
$doc = new SimpleXmlElement(get_web_page("https://rest.api.pbsmartconnections.com/v1/Lists")); | |
$info = new SimpleXmlElement (get_web_page("https://rest.api.pbsmartconnections.com/v1/Subscribers/$_GET[original_email]/Properties")); | |
print "<pre>"; | |
var_dump(new SimpleXmlElement(get_web_page('https://rest.api.pbsmartconnections.com/v1/Ping'))); | |
print "</pre>"; | |
//define variables for xml data | |
$email = strval($info->EmailAddress); | |
//var_dump(strval($info->EmailAddress)); | |
foreach($info->CustomFields->CustomField as $CustomField) { | |
switch($CustomField->FieldName){ | |
case 'CompanyName': | |
$CompanyName = $CustomField->Value; | |
break; | |
case 'FullName': | |
$FullName = $CustomField->Value; | |
break; | |
case 'City': | |
$City= $CustomField->Value; | |
break; | |
case 'State': | |
$State= $CustomField->Value; | |
break; | |
case 'Zip': | |
$Zip= $CustomField->Value; | |
break; | |
case 'Country': | |
$Country= $CustomField->Value; | |
break; | |
} | |
} | |
$user_list_array = array(); | |
foreach($info->Lists->List as $List) { | |
$List_id = intval($List->ListID); | |
$user_list_array[] = $List_id; | |
} | |
if(isset($_GET['subscription_submit'])) { | |
$Val = True; | |
// Set variables for nonrequired fields | |
$City = empty($_GET['City']) ? "" : $_GET['City']; | |
$State = empty($_GET['State']) ? "" : $_GET['State']; | |
//start validation code..Error messages | |
if(empty($_GET["CompanyName"])) { | |
$CompanyNameErr = "Missing"; | |
$MessageErr= "Please fill out the required fields."; | |
$Val= False; | |
} else { | |
$CompanyName=$_GET["CompanyName"]; | |
} | |
if(empty($_GET["FullName"])) { | |
$FullNameErr = "Missing"; | |
$MessageErr= "Please fill out the required fields."; | |
$Val= False; | |
} else { | |
$FullName=$_GET["FullName"]; | |
} | |
if(empty($_GET["Zip"])) { | |
$ZipErr = "Missing"; | |
$MessageErr= "Please fill out the required fields."; | |
$Val= False; | |
} else { | |
$Zip=$_GET["Zip"]; | |
} | |
if(empty($_GET["Country"])) { | |
$CountryErr = "Missing"; | |
$MessageErr= "Please fill out the required fields."; | |
$Val= False; | |
} else { | |
$Country=$_GET["Country"]; | |
} | |
if(empty($_GET["email"])) { | |
$emailErr = "Missing"; | |
$MessageErr= "Please fill out the required fields."; | |
$Val= False; | |
} else { | |
$email=$_GET["email"]; | |
} | |
// Validate at least one list was chosen. | |
$user_list_array = array(); | |
foreach ($doc as $i) { | |
$listID = intval($i->ListID); | |
if (isset($_GET["list_$listID"])) { | |
$user_list_array[] = $_GET["list_$listID"]; | |
} | |
} | |
if (empty($user_list_array)) { | |
$valErr = "**Please select 1 or more."; | |
$Val= False; | |
} | |
//check the state of the form to see if it's valid if not. | |
if ($Val == True) { | |
// Setup an array based on the lists from PB. | |
$list_array = array(); | |
foreach ($doc as $i) { | |
$listID = intval($i->ListID); | |
if (isset($_GET["list_$listID"])){ | |
$list_array[] = $listID; | |
} | |
} | |
//Define additional field Variables | |
$info = array( | |
'EmailAddress' => $email, | |
'CustomFields'=> array ( | |
array ( | |
'FieldName' => 'CompanyName', | |
'Value' => $CompanyName), | |
array ( | |
'FieldName' => 'FullName', | |
'Value' => $FullName), | |
array ( | |
'FieldName' => 'City', | |
'Value' => $City), | |
array ( | |
'FieldName' => 'State', | |
'Value' => $State), | |
array ( | |
'FieldName' => 'Zip', | |
'Value' => $Zip), | |
array ( | |
'FieldName' => 'Country', | |
'Value' => $Country), | |
), | |
"Lists" => $list_array | |
); | |
//convert php to json string and store string into variable $json | |
$json = json_encode($info); | |
$URL = "https://rest.api.pbsmartconnections.com/v1/Subscribers/$_GET[original_email]/Properties"; | |
print "<pre>"; | |
var_dump($json); | |
print "</pre>"; | |
//show restults | |
$response= send_data("https://rest.api.pbsmartconnections.com/v1/Subscribers/$_GET[original_email]", $json); | |
print "<pre>"; | |
var_dump($response); | |
print "</pre>"; | |
} | |
}; | |
?> | |
<!--BOF HTML Form --> | |
<p>We appreciate your interest in receiving Newhaven Display's newsletter.</p> | |
<form action="<?php print $_SERVER['PHP_SELF']; ?>" method="GET"> | |
<p> * Required field.</p> | |
<br/> | |
<fieldset> | |
<legend>Your Current Subscription Information</legend> | |
<br/> | |
<label for="CompanyName"> | |
Company: <input type="text" name="CompanyName" size="44" id="CompanyName" value="<?php echo ($CompanyName);?>"/> * | |
<? echo $CompanyNameErr;?> | |
</label> | |
<br/> | |
<br/> | |
<label for="FullName"> | |
First & Last Name: <input type="text" name="FullName" size="44" id="FullName" value="<?php echo ($FullName);?>"/> * | |
<? echo $FullNameErr;?> | |
</label> | |
<br/> | |
<label for="City"> | |
City: <input type="text" name="City" size="44" id="City" value="<?php echo ($City);?>"/> | |
</label> | |
<br/> | |
<label for="State"> | |
State: <input type="text" name="State" size="44" id="State" value="<?php echo ($State);?>"/> | |
</label> | |
<br/> | |
<label for="Zip"> | |
Zip Code: <input type="text" name="Zip" size="44" id="Zip" value="<?php echo ($Zip);?>"/> * | |
<? echo $ZipErr;?> | |
</label> | |
<br/> | |
<label for="Country"> | |
Country: <input type="text" name="Country" size="44" id="Country" value="<?php echo ($Country);?>"/> * | |
<? echo $CountryErr;?> | |
</label> | |
<br/> | |
<br/> | |
<label for="email"> | |
Email: <input type="text" name="email" size="44" id="email" value="<?php echo ($email);?>"/> * | |
<? echo $emailErr;?> | |
</label> | |
<!--invisible field for original email value --> | |
<input type="hidden" name="original_email" value="<?php print $_GET['original_email']; ?>" /> | |
<br/> | |
<br/> | |
<p>Below are the publications that you are currently subscribed to. To make a change, only select the names of the publications you wish to receive.</p> | |
<br/> | |
<br/> | |
<?php | |
print "<pre>"; | |
var_dump($user_list_array); | |
print "</pre>"; | |
// Starts iterating over the full set of Lists | |
foreach($doc->List as $List) { | |
// Creates a variable that olds the ListID as an integer/number. | |
$list_id = intval($List->ListID); | |
// Starts the HTML that makes up the checkbox, uses the ListID as a unique identifier in 'for' | |
$html = "<label for='list_" . $list_id . "'>"; | |
// .= is a way to add more into a variable without cleaning out the current value, this line adds the Name of the list. | |
$html .= strval($List->Name); | |
// Start the input field, using the same naming scheme for the 'name' and the 'id' as the 'for' in the label. | |
$html .= "<input type='checkbox' value='" . $list_id . "' name='list_" . $list_id . "' id='list_" . $list_id . "' "; | |
// Check to see if the ListID is in the users list using in_array, | |
// If in_array returns true, which it will if the ListID also shows up in the $user_list_array, which means | |
// that the subscriber is already on that list. | |
if (in_array($list_id, $user_list_array)) { | |
// They're on the list, so add the 'checked' property to make the checkbox checked. | |
$html .= "checked='checked'"; | |
} | |
// Close the input field tag. | |
$html .= " />"; | |
// Close the label tag. | |
$html .= "</label>"; | |
// print out the resulting html. | |
print $html; | |
} | |
echo $valErr; | |
?> | |
</fieldset> | |
<br/> | |
<input type="submit" name="subscription_submit" value="Update Preferences"> | |
</form> | |
<!--EOF Creating Content for HTML Form --> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment