【问题标题】:Variable not having effect in PHP function when placed within XML [closed]放置在 XML 中时,变量在 PHP 函数中不起作用 [关闭]
【发布时间】:2013-06-11 01:42:32
【问题描述】:

我有一个需要使用 PHP 提交的 xml。从 xml 中的 3 个 PHP 变量来看,$shippingMode 是一个字符串,它没有被正确传递。我尝试了多种方法,但没有任何帮助。代码如下:

$zip = 90002;
$pounds = 0.1;
$shippingMode = "Express";

function USPSParcelRate($pounds,$zip) {
$url = "http://production.shippingapis.com/shippingAPI.dll";

$devurl ="testing.shippingapis.com/ShippingAPITest.dll";
$service = "RateV4";
$xml = rawurlencode("<RateV4Request USERID='USER' >
<Revision/>
     <Package ID='1ST'>
          <Service>'".$shippingMode."'</Service>
          <ZipOrigination>10025</ZipOrigination>
          <ZipDestination>".$zip."</ZipDestination>
          <Pounds>".$pounds."</Pounds>
          <Ounces>0</Ounces>
          <Container></Container>
          <Size>REGULAR</Size>
          <Width></Width>
          <Length></Length>
          <Height></Height>
          <Girth></Girth>
     </Package>
</RateV4Request>");

我也试过直接放置 $shippingMode 而不连接。或者只是".$shippingMode."

知道在 XML 中使用字符串最安全和最合适的方法是什么吗?

【问题讨论】:

标签: php xml string variables concatenation


【解决方案1】:

您在USPSParcelRate() 函数的范围之外分配了$shippingMode 变量。为了在函数中使用它,您需要将它作为参数传递:

function USPSParcelRate($pounds,$zip,$shippingMode) {
    ...
}

编辑:

您发布的代码在函数上缺少右花括号,因此如果未将其重新添加,则会引发错误。这是完整的代码,包括声明后函数的调用:

<?php

function USPSParcelRate($pounds,$zip,$shippingMode) {

    $url = "http://production.shippingapis.com/shippingAPI.dll";
    $devurl ="testing.shippingapis.com/ShippingAPITest.dll";
    $service = "RateV4";
    $xml = "<RateV4Request USERID='USER'>
    <Revision/>
        <Package ID='1ST'>
            <Service>'".$shippingMode."'</Service>
            <ZipOrigination>10025</ZipOrigination>
            <ZipDestination>".$zip."</ZipDestination>
            <Pounds>".$pounds."</Pounds>
            <Ounces>0</Ounces>
            <Container></Container>
            <Size>REGULAR</Size>
            <Width></Width>
            <Length></Length>
            <Height></Height>
            <Girth></Girth>
        </Package>
    </RateV4Request>";

    print_r($xml); // for debugging

}

$zip = 90002;
$pounds = 0.1;
$shippingMode = "Express";

USPSParcelRate($pounds,$zip,$shippingMode); // function invocation

?>

【讨论】:

  • 谢谢!如何打印我应该在响应中获得的特定节点 RATE?
【解决方案2】:

你没有在你的函数中调用它......

它需要作为参数添加。 就这样……

function USPSParcelRate($pounds,$zip,$shippingMode) {

 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多