放在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?

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

?>

Youre not calling it into your function....

It needs to be added as an argument. Like so....

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

 }