2012-01-19 64 views
0

所以我试图用一个类,我发现解决了电子商务的问题,我有。使用simplexml_load_file()问题

Original Blog Post

在华盛顿州的在线商店需要确定的基础上,送货地址税率。

WA Sales Tax Rate Lookup URL Interface

类别:

/** 
* @author SmallDog 
* @contact [email protected] 
* @created 01-27-2011 
**/ 

class destinationTax 
{ 
    private $dor_url = 'http://dor.wa.gov'; 

    function __construct(){ } 

    function getTax($addr,$city,$zip) 
    { 
     $req = $this->dor_url."/AddressRates.aspx?output=xml&addr=$addr&city=$city&zip=$zip"; 
     return $this->_get_decoded($req); 
    } 

    private function _get_decoded($url) 
    { 
     $url = urlencode($url); 
     if($xml = simplexml_load_file($url)) 
     { 
      switch($xml->attributes()->code) 
      { 
       case 0: 
        // Code 0 means address was perfect 
        break; 
       case 1: 
        $xml->msg = "Warning: The address was not found, but the ZIP+4 was located."; 
        break; 
       case 2: 
        $xml->msg = "Warning: Neither the address or ZIP+4 was found, but the 5-digit ZIP was located."; 
        break; 
       case 3: 
        $xml->msg = "Error: The address, ZIP+4, and ZIP could not be found."; 
        break; 
       case 4: 
        $xml->msg = "Error: Invalid arguements."; 
        break; 
       case 5: 
        $xml->msg = "Error: Internal error."; 
      } 
     } 
     else $xml = "Error: Could not load XML."; 

     return $xml; 
    } 
} 

用途:

$tax = new destinationTax; 
$tax = $tax->getTax("123 Main Street", "Kirkland", "98033"); 
echo $tax->attributes()->rate; 

错误:

Warning: simplexml_load_file() [function.simplexml-load-file]: URL file-access is disabled in the server configuration in /.../.../classes.php

Warning: simplexml_load_file(http://dor.wa.gov/AddressRates.aspx?output=xml&addr=123+Main+Street&city=Kirkland&zip=98033) [function.simplexml-load-file]: failed to open stream: no suitable wrapper could be found in /.../.../classes.php

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http%3A%2F%2Fdor.wa.gov%2FAddressRates.aspx%3Foutput%3Dxml%26addr%3D123+Main+Street%26city%3DKirkland%26zip%3D98033" in /.../.../classes.php

Fatal error: Call to a member function attributes() on a non-object in /.../.../tax.php

+3

'URL文件访问服务器configuration'禁用是比较明确的...请访问http:// PHP。 net/manual/en/function.fopen.php的解释 –

回答

5

您的服务器不支持远程文件访问。如果你有卷曲的访问,你可以得到XML数据这种方式,如下所示例:

$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
$data = curl_exec($curl); 

$xml = simplexml_load_string($data); 

// ... 
+0

你是对的...我的本地机器工作...谢谢 – taylorjes