Connecting to API Using PHP

php-Logo Included here you will find simple sample code for parsing the Database Interface API in XML. The first example returns primary data for a given jurisdiction - the result containing the United States Post Office. Some ZIP codes include multiple cities and sometimes multiple tax jurisdictions, so the second example shows how to go down the hierarchy and parse the XML.

Basic: mssql-basic.php

		<?php
$username = "sample";
$password = "password";
$zipcode = "90210";
$url = "https://api.zip2tax.com/TaxRate-USA.xml?username=" . $username . "&password=" . $password . "&zip=" . $zipcode;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
/*echo $curl_response;*/
curl_close($curl);

$curl_response = str_replace("utf-16","utf-8",$curl_response); 
$doc = new DOMDocument();
$doc->loadXML($curl_response);

$addrs = $doc->getElementsByTagName( "address" );

foreach ($addrs as $addr):
        $temp = $addr->getElementsByTagName( "zipCode" );
        $zip= $temp->item(0)->nodeValue;
        
        $temp = $addr->getElementsByTagName( "taxRate" );
        $salesTax= $temp->item(0)->nodeValue;
        
        $temp = $addr->getElementsByTagName( "place" );
        $place= $temp->item(0)->nodeValue;
        
        $temp = $addr->getElementsByTagName( "county" );
        $county= $temp->item(0)->nodeValue;
        
        $temp = $addr->getElementsByTagName( "state" );
        $state= $temp->item(0)->nodeValue;
endforeach;

$notes = $doc->getElementsByTagName( "notes" );
foreach ($addrs as $addr):
        $temp = $addr->getElementsByTagName( "note" );
        $notes= $temp->item(1)->nodeValue;
endforeach;

echo "Zip Code: $zip" ;
echo "\n";
echo "Sales Tax Rate: $salesTax" ;
echo "\n";
echo "Post Office City: $place" ;
echo "\n";
echo "County: $county" ;
echo "\n";
echo "State: $state" ;
echo "\n";
if($notes == "Shipping charges are not taxable")
{
  echo "Shipping Taxable: 1" ;
}
else
{
  echo "Shipping Taxable: 0" ;
}

echo $doc->errorInfo->errorMessage[0];

?>

		

 

Breakout: mssql-breakout.php

		<?php
$username = "sample";
$password = "password";
$zipcode = "90210";
$url = "https://api.zip2tax.com/TaxRate-USA.xml?username=" . $username . "&password=" . $password . "&zip=" . $zipcode;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
/*echo $curl_response;*/
curl_close($curl);

$curl_response = str_replace("utf-16","utf-8",$curl_response); 
$doc = new DOMDocument();
$doc->loadXML($curl_response);

$addrs = $doc->getElementsByTagName( "address" );

foreach ($addrs as $addr):
        $temp = $addr->getElementsByTagName( "zipCode" );
        $zip= $temp->item(0)->nodeValue;
        
        $temp = $addr->getElementsByTagName( "taxRate" );
        $salesTax= $temp->item(0)->nodeValue;
        
        $temp = $addr->getElementsByTagName( "place" );
        $place= $temp->item(0)->nodeValue;
        
        $temp = $addr->getElementsByTagName( "county" );
        $county= $temp->item(0)->nodeValue;
        
        $temp = $addr->getElementsByTagName( "state" );
        $state= $temp->item(0)->nodeValue;
endforeach;

$notesList = $doc->getElementsByTagName( "notes" );
foreach ($notesList as $addr):
        $temp = $addr->getElementsByTagName( "note" );
        $notes= $temp->item(1)->nodeValue;
endforeach;

$rateList = $doc->getElementsByTagName( "rateDetails" );
foreach ($rateList as $addr):
        $temp = $addr->getElementsByTagName( "taxRate" );
        $tax_state= $temp->item(0)->nodeValue;
        $tax_county= $temp->item(1)->nodeValue;
        $tax_city= $temp->item(2)->nodeValue;
        $tax_special= $temp->item(3)->nodeValue;
endforeach;

echo "Zip Code: $zip" ;
echo "\n";
echo "Sales Tax Rate: $salesTax" ;
echo "\n";
echo "Post Office City: $place" ;
echo "\n";
echo "County: $county" ;
echo "\n";
echo "State: $state" ;
echo "\n";
if($notes == "Shipping charges are not taxable")
{
  echo "Shipping Taxable: 1" ;
}
else
{
  echo "Shipping Taxable: 0" ;
}

echo "Sales Tax Rate State: $tax_state" ;
echo "\n";
echo "Sales Tax Rate County: $tax_county" ;
echo "\n";
echo "Sales Tax Rate City: $tax_city" ;
echo "\n";
echo "Sales Tax Rate Special: $tax_special" ;
echo "\n";

?>

		

 

Sample ZIP Code Range

In the sample ZIP code range (90001 - 90999), the following ZIP codes contain multiple tax rates; which can be used to test the return of multiple rows:

  • 90304
  • 90601
  • 90631
Terms of service: At Zip2Tax, your security and confidentiality are our highest priority. We will never share your e-mail address or any other personally identifiable information (PII) with any other person or company. Please review our full privacy policy. License: Please refer to our end user license agreement. Cancellation and Refund Policy: Please refer to our refund policy prior to purchase.
Copyright © 2006-2018 Zip2Tax, LLC. All rights reserved. Zip2Tax and Spot-On™ are trademarks of Zip2Tax, LLC, reg. pending U.S. Pat. & Tm. Off., S/N 86/613550 and 86/613547.

Quantcast
Quantcast
IP Address: 35.171.146.16
JavaScript: Fail
loggedd: none
WSPhilly03/DBPhilly03/CCProd

Don't have an account?

Sign up for a free trial here
If you do not have an account, please provide the information below.


Already have an account?
Free trial close Thank You!

Please check your email for your Username and Password.

If you do not receive your email shortly, check your SPAM folder for mail from "info@Zip2Tax.com" or call us at 1-866-492-8494, Monday - Friday 8am - 5pm EST.
Zip2Tax Password recovery close Password recovery

Please provide your registered email to verify and send password.



submit