Posted: October 27th, 2009 | Author: jriggs | Filed under: php | Tags: address, php, standard, usps, web tools, zip code | 8 Comments »
Before using this code you will need to sign up for an account with USPS webtools here.
Class, save as ‘usps_address_class.php’
<?php
class usps {
public $account = 'xxxxxxx'; //you need to register for this
public $url = 'http://production.shippingapis.com/ShippingAPI.dll';
public $address1, $address2, $city, $state, $zip;
public $ship_address1, $ship_address2, $ship_city, $ship_state, $ship_zip;
function toXML() {
$xml = ' <AddressValidateRequest USERID="' . $this->account . '"><Address ID="1">';
$xml .= '<Address1>' . $this->address1 . '</Address1>';
$xml .= '<Address2>' . $this->address2 . '</Address2>';
$xml .= '<City>' . $this->city . '</City>';
$xml .= '<State>' . $this->state . '</State>';
$xml .= '<Zip5>' . $this->zip . '</Zip5>';
$xml .= '<Zip4></Zip4></Address>';
if ($this->ship_address2 <> ''){
//shipping address
$xml .= '<Address ID="2">';
$xml .= '<Address1>' . $this->ship_address1 . '</Address1>';
$xml .= '<Address2>' . $this->ship_address2 . '</Address2>';
$xml .= '<City>' . $this->ship_city . '</City>';
$xml .= '<State>' . $this->ship_state . '</State>';
$xml .= '<Zip5>' . $this->ship_zip . '</Zip5>';
$xml .= '<Zip4></Zip4></Address>';
}
$xml .= '</AddressValidateRequest>';
return $xml;
}
function submit_request() {
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "API=Verify&XML=" . $this->toXML());
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($ch);
$error = curl_error($ch);
if(empty($error)) {
return $result;
}else{
die(curl_error($ch));
}
}
}
?>
And a sample page accessing it:
<?php
require("usps_address_class.php");
$uspsRequest = new usps(); //class instantiation
$uspsRequest->address1 = 'suite 321';
$uspsRequest->address2 = '1600 Pennsylvania Ave NW';
$uspsRequest->city = 'Washington';
$uspsRequest->state = 'DC';
$uspsRequest->zip = '20500';
/*
//optional second address
$uspsRequest->ship_address1 = '';
$uspsRequest->ship_address2 = '';
$uspsRequest->ship_city = '';
$uspsRequest->ship_state = '';
$uspsRequest->ship_zip = '';
*/
$result = $uspsRequest->submit_request();
if (!empty($result)){
$xml = new SimpleXMLElement($result);
}else{
die;
}
if(isset($xml->Address[0]->Error)) { echo ' Error Address 1';}
if(isset($xml->Address[1]->Error)) { echo ' Error Address 2';}
echo $xml->Address[0]->Address2 . ' ' . $xml->Address[0]->Address1 ;
echo '<br />';
echo $xml->Address[0]->City. ' ' . $xml->Address[0]->State . ' ' . $xml->Address[0]->Zip5;
echo '<br />';
echo $xml->Address[1]->Address2 . ' ' . $xml->Address[1]->Address1 ;
echo '<br />';
echo $xml->Address[1]->City. ' ' . $xml->Address[1]->State . ' ' . $xml->Address[1]->Zip5;
?>
Posted: August 6th, 2009 | Author: jriggs | Filed under: php | Tags: encrypt, php, string | 5 Comments »
The following code represents an easy way to create an encrypted string that can be passed in an URL. While this method is adequate for email addresses, user names, and the like – it should not be used to encrypt mission-critical info or personal data such as Social Security numbers or credit card information.
Also, you will need to have the ‘mcrypt’ module enabled. You can do this by opening your php.ini file and removing the ‘;’ just before ‘extension=php_mcrypt.dll’.
<?php
$cc=mencrypt("This is the string to be encrypted","key-change-this");
echo $cc.'<br />';
$dd=mdecrypt($cc,"key-change-this");
echo $dd.'<br />';
//encrypted value passed in url ie:
//http://yoursite.com/crypted.php?id=encrypted_string
if (isset($_GET["id"])){
$ee=$_GET["id"];
echo $ee.'<br />';
echo mdecrypt($ee,"key-change-this");
}
function mencrypt($input,$key){
$key = substr(md5($key),0,24);
$td = mcrypt_module_open ('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $key, $iv);
$encrypted_data = mcrypt_generic ($td, $input);
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);
return trim(chop(url_base64_encode($encrypted_data)));
}
function mdecrypt($input,$key){
$input = trim(chop(url_base64_decode($input)));
$td = mcrypt_module_open ('tripledes', '', 'ecb', '');
$key = substr(md5($key),0,24);
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $key, $iv);
$decrypted_data = mdecrypt_generic ($td, $input);
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);
return trim(chop($decrypted_data));
}
function url_base64_encode($str){
return strtr(base64_encode($str),
array(
'+' => '.',
'=' => '-',
'/' => '~'
)
);
}
function url_base64_decode($str){
return base64_decode(strtr($str,
array(
'.' => '+',
'-' => '=',
'~' => '/'
)
));
}
?>
End Code.
Posted: July 8th, 2009 | Author: jriggs | Filed under: php, wordpress | Tags: blank page, wordpress | 18 Comments »
There may come a time when you need to create a blank page on your WordPress site that still uses your template’s header, footer and sidebar. For example, if you incorporate your own custom search from google, you will need to specify a landing page where the search results will be displayed.
You could use WordPress to create a new page, but this has some problems.
- WordPress tends to add code to the page the you don’t need or want
- You will need to recreate this file whenever you switch themes or upgrade.
- You cannot easily control where this file is created.
To avoid these problems we can use some built-in functions to display a blank page that uses your template and gives you greater control of the page’s content.
First, create a new file in the root directory of your blog (the directory that contains ‘wp-blog-header.php’). Name it whatever makes sense to you but make sure to give it the ‘.php’ extension. Copy the following into the file you have just created:
<?php
define('WP_USE_THEMES', false);
require('wp-blog-header.php');
get_header();
get_sidebar();
?>
<p>Hello!</p>;
<?php
get_footer();
?>
You can see an example of this page here – http://joe-riggs.com/blog/blank.php. You can add any valid html or javascript to the section between the php tags and will be displayed. If you don’t want to display the header, sidebar or footer you can simply comment out or remove that particular line – ie : delete entire “get_sidebar();” line to remove the sidebar.
Posted: March 11th, 2009 | Author: jriggs | Filed under: php | Tags: payload, php, post, secure, xml | 1 Comment »
Sample requires curl see code below:
function callWebServicePost($host, $url, $payload, $user, $password) {
$header[] = "Host: ". $host;
$header[] = "Content-type: text/xml";
$header[] = "Content-length: ".strlen($payload) . "\r\n";
$header[] = $payload;
$session = curl_init($url);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_CONNECTTIMEOUT,30);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, '');
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, true);
// Display headers
curl_setopt($session, CURLOPT_VERBOSE, true);
// Display communication with server
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Return data instead of display to std out
curl_setopt($session, CURLOPT_HTTPHEADER, $header );
// headers from above
// provide credentials if they're established
if(!empty($user) && !empty($password))
curl_setopt($session, CURLOPT_USERPWD, $user . ":" . $password);
// tell cURL to graciously accept an SSL certificate if presented
if(ereg("^(https)",$url))
curl_setopt($session, CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($session);
curl_close($session);
return $result;
}