我正在尝试将 PHP 数组输出转换为一个数字 PHP 变量,以便我可以对其进行算术运算,然后将其全局存储以在购物车中使用。
这是生成和操作数组的代码:
<?php
function ups($dest_zip,$service,$weight,$length,$width,$height) {
$AccessLicenseNumber = 'XXXXXXXXXXXXX'; // Your license number
$UserId = 'XXXXXXXX'; // Username
$Password = 'XXXXXXXX'; // Password
$PostalCode = 'XXXXXX'; // Zipcode you are shipping FROM
$ShipperNumber = 'XXXXX'; // Your UPS shipper number
$data ="<?xml version=\"1.0\"?>
<AccessRequest xml:lang=\"en-US\">
<AccessLicenseNumber>$AccessLicenseNumber</AccessLicenseNumber>
<UserId>$UserId</UserId>
<Password>$Password</Password>
</AccessRequest>
<?xml version=\"1.0\"?>
<RatingServiceSelectionRequest xml:lang=\"en-US\">
<Request>
<TransactionReference>
<CustomerContext>Bare Bones Rate Request</CustomerContext>
<XpciVersion>1.0001</XpciVersion>
</TransactionReference>
<RequestAction>Rate</RequestAction>
<RequestOption>Rate</RequestOption>
</Request>
<PickupType>
<Code>01</Code>
</PickupType>
<Shipment>
<Shipper>
<Address>
<PostalCode>$PostalCode</PostalCode>
<CountryCode>US</CountryCode>
</Address>
<ShipperNumber>$ShipperNumber</ShipperNumber>
</Shipper>
<ShipTo>
<Address>
<PostalCode>$dest_zip</PostalCode>
<CountryCode>US</CountryCode>
<ResidentialAddressIndicator/>
</Address>
</ShipTo>
<ShipFrom>
<Address>
<PostalCode>$PostalCode</PostalCode>
<CountryCode>US</CountryCode>
</Address>
</ShipFrom>
<Service>
<Code>$service</Code>
</Service>
<Package>
<PackagingType>
<Code>02</Code>
</PackagingType>
<Dimensions>
<UnitOfMeasurement>
<Code>IN</Code>
</UnitOfMeasurement>
<Length>$length</Length>
<Width>$width</Width>
<Height>$height</Height>
</Dimensions>
<PackageWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>$weight</Weight>
</PackageWeight>
</Package>
</Shipment>
</RatingServiceSelectionRequest>";
$ch = curl_init("https://www.ups.com/ups.app/xml/Rate");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_TIMEOUT, 60);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$result=curl_exec ($ch);
$upsout = (explode('USD', $result, 15));
$handling = $upsout[3];
echo "<br><br>Shipping: $";
echo $handling;
echo " USD<br><br>";
curl_close($ch);
var_dump( $handling);
// print_r ($upsout);}?>
$handling 的输出始终是 00.00 或 000.00 格式的数字,当然,零是数字。我真的需要它才能在算术函数中工作并能够存储以用作数字。
我知道它不起作用,因为如果我这样做
$test = $handling *2;
echo $test;
我得到“0”作为输出,而不是 $handling 的两倍......
编辑:
如果我运行 var_dump(htmlentities($handling));
输出是:
字符串(222)“33.35”
如果我运行 var_dump($handling);
输出是
字符串(168)“33.35”
所以....我需要找到一种方法来解析这个数组条目中的垃圾,这样我就可以代数地操纵它。