PHP类中的simplexml_load_file()问题

时间:2012-01-19 23:33:49

标签: php simplexml

所以我正在尝试使用我发现的课程来修复我的电子商务问题。

Original Blog Post

在华盛顿州,网上商店需要根据送货地址确定税率。

WA Sales Tax Rate Lookup URL Interface

类别:

/**
 * @author SmallDog
 * @contact dustin@smalldo.gs
 * @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;

错误:

  

警告:simplexml_load_file()[function.simplexml-load-file]:URL   在服务器配置中禁用文件访问   /.../.../ classes.php

     

警告:   使用simplexml_load_file(http://dor.wa.gov/AddressRates.aspx?output=xml&addr=123+Main+Street&city=Kirkland&zip=98033)   [function.simplexml-load-file]:无法打开流:不合适   包装器可以在/.../.../ classes.php

中找到      

警告:simplexml_load_file()[function.simplexml-load-file]:I / O   警告:无法加载外部实体   的 “http%3A%2F%2Fdor.wa.gov%2FAddressRates.aspx%3Foutput%3DXML%26addr%3D123 +主+街%26city%3DKirkland%26zip%3D98033”   在/.../.../ classes.php

     

致命错误:在非对象中调用成员函数attributes()    /.../.../ tax.php

1 个答案:

答案 0 :(得分:6)

您的服务器不支持远程文件访问。如果您具有cURL访问权限,则可以通过以下方式获取XML数据:

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

$xml = simplexml_load_string($data);

// ...