0

所以我试图在php中集成fedex for web services。在此之前的表格获取包裹接收者的所有必要物品。我的所有代码一直有效,直到我尝试从表单中添加 $postal 变量,如果我将 85308 硬编码为 'PostalCode' => '85308' 它可以工作,但如果我将 '85308' 替换为 ''.$邮政。''它不起作用。它给了我错误消息:目的地邮政编码丢失或无效。

我在变量上使用了 trim() 更改了它的名称,所有这些好东西仍然不行。

任何想法或意见将不胜感激我的代码如下。

谢谢

<?php
//get address information$fname = $_POST["fname"];
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$state = $_POST["state"];
$address1 = $_POST["address1"];
$address2 = $_POST["address2"];
$postal = $_POST["zip"];
$city = $_POST["city"];
$fullname = $fname." ".$lname;
$fulladdress = $address1." ".$address2;

function addRecipient(){
    $recipient = array(
        'Contact' => array(
            'PersonName' => ''.$fullname.'',
            'CompanyName' => 'Company Name',
            'PhoneNumber' => '9012637906'
        ),
        'Address' => array(
            'StreetLines' => array(''.$fulladdress.''),
            'City' => ''.$city.'',
            'StateOrProvinceCode' => ''.$state.'',
            //'PostalCode' => '85308',
            'PostalCode' => ''.$postal.'',
            'CountryCode' => 'US',
            'Residential' => false)
    );
    return $recipient;                  
}
4

1 回答 1

1

在函数内部,变量在您的范围之外。见http://php.net/manual/en/language.variables.scope.php

解决方案:

  • Import the variables inside the function
  • Import the variables as parameters to your function
  • Use globals (ugly)
于 2012-03-21T00:00:51.970 回答