douchengchen7959 2013-06-11 01:42
浏览 21
已采纳

放在XML中时,变量在PHP函数中没有效果[关闭]

I have a xml that I need to submit using PHP. From the 3 PHP variables in the xml, $shippingMode is a string and it's not being passed properly. I've tried multiple ways but nothing helps. Here is the code:

$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>");

I've also tried putting $shippingMode directly without concatenating. Or just ".$shippingMode."

Any idea of which is the safest and proper way to have a string within the XML?

  • 写回答

2条回答 默认 最新

  • douque9982 2013-06-11 01:53
    关注

    You're assigning the $shippingMode variable outside of the scope of the USPSParcelRate() function. In order to use it within the function, you'll need to pass it as an argument:

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

    EDIT:

    Your code, as posted, is missing a closing curly brace on the function, so that'll throw an error if it's not added back in. Here's the full code, including the invocation of the function after declaration:

    <?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
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?