WordPress:表单发布但PHP脚本没有执行

时间:2016-12-07 19:34:23

标签: php html wordpress forms post

我的目标是让用户填写包含国家/地区和邮政编码的表单,将数据发布到调用fedex服务器并返回发货率的php脚本。

我已经在我的本地Web服务器上成功完成了此操作,但是在我的WordPress站点上实现后,php脚本在发布表单数据时不会执行。我的代码如下:

rate.php(此文件中的表单和php脚本):

<!-- FORM -->
<h1 style="font-family: "Ek Mukta", sans-serif;">Quick Quote</h1>
<br>
<form id="quick-quote" class="" action="" method="post">
  <label>Country</label>
  <br>
  <input id="country" type="text" name="country" value="">
  <br>
  <label>Postal Code</label>
  <br>
  <input id="postal" type="text" name="postal" value="">
  <br>
  <br>
  <input id="submit" type="submit" name="" value="submit">
</form>
<br>

<!-- FEDEX CALL -->
<?php
print_r($_POST); //this works!
error_reporting(E_ALL);
session_start();

require_once('../../library/fedex-common.php'); //code seems to break here

$newline = "<br />";
//The WSDL is not included with the sample code.
//Please include and reference in $path_to_wsdl variable.
$path_to_wsdl = "RateService/RateService_v20.wsdl";

ini_set("soap.wsdl_cache_enabled", "0");

$client = new SoapClient($path_to_wsdl, array('trace' => 1)); // Refer to http://us3.php.net/manual/en/ref.soap.php for more information

$request['WebAuthenticationDetail'] = array(
    'UserCredential' =>array(
        'Key' => getProperty('key'),
        'Password' => getProperty('password')
    )
);
$request['ClientDetail'] = array(
    'AccountNumber' => getProperty('shipaccount'),
    'MeterNumber' => getProperty('meter')
);
$request['TransactionDetail'] = array('CustomerTransactionId' => ' *** Rate Request v20 using PHP ***');
$request['Version'] = array(
    'ServiceId' => 'crs',
    'Major' => '20',
    'Intermediate' => '0',
    'Minor' => '0'
);
$request['ReturnTransitAndCommit'] = true;

$request['RequestedShipment']['DropoffType'] = 'REGULAR_PICKUP'; // valid values REGULAR_PICKUP, REQUEST_COURIER, ...
$request['RequestedShipment']['ShipTimestamp'] = date('c');
$request['RequestedShipment']['ServiceType'] = 'INTERNATIONAL_ECONOMY'; // valid values STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, ...
$request['RequestedShipment']['PackagingType'] = 'YOUR_PACKAGING'; // valid values FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, ...
$request['RequestedShipment']['TotalInsuredValue']=array('Ammount'=>0,'Currency'=>'USD');
$request['RequestedShipment']['Shipper'] = addShipper();
$request['RequestedShipment']['Recipient'] = addRecipient();
$request['RequestedShipment']['VariableHandlingChargeDetail'] = addHandlingCharge();
$request['RequestedShipment']['ShippingChargesPayment'] = addShippingChargesPayment();
//$request['RequestedShipment']['RateRequestTypes'] = addRateType();
$request['RequestedShipment']['RateRequestTypes'] = 'LIST';
// $request['RequestedShipment']['RateRequestTypes'] = 'PREFERRED ';
$request['RequestedShipment']['PackageCount'] = '1';
$request['RequestedShipment']['RequestedPackageLineItems'] = addPackageLineItem1();
try
{
  if(setEndpoint('changeEndpoint'))
  {
      $newLocation = $client->__setLocation(setEndpoint('endpoint'));
  }

   $response = $client ->getRates($request);

  if ($response -> HighestSeverity != 'FAILURE' && $response -> HighestSeverity != 'ERROR')
  {
      $rateReply = $response -> RateReplyDetails;
      echo '<table>';

    $country = $_POST['country'];
    $postal = $_POST['postal'];
    $productPrice = 100;
    $serviceFee = 50;
    echo $country;

      // echo '<tr><td>Country Code </td><td>Postal Code</td><td>Service Type</td><td>Amount</td><td>Delivery Date</td></tr><tr>';
      $serviceType = '<td>'.$rateReply -> ServiceType . '</td>';
      $amount = number_format($rateReply->RatedShipmentDetails[0]->ShipmentRateDetail->TotalVariableHandlingCharges->TotalCustomerCharge->Amount,2,".",",");
      if(array_key_exists('DeliveryTimestamp',$rateReply)){
          $deliveryDate= '<td>' . $rateReply->DeliveryTimestamp . '</td>';
      }else if(array_key_exists('TransitTime',$rateReply)){
          $deliveryDate= '<td>' . $rateReply->TransitTime . '</td>';
      }else {
          $deliveryDate='<td>&nbsp;</td>';
      }
      $total = $productPrice + $serviceFee + $amount;
      // echo "<td>$country</td>. <td>$postal</td>. $serviceType. <td>$amount</td>. $deliveryDate";
      // echo '</tr>';
      // echo '</table>';

      echo
      "
      <h3>Country Code: $country</h3>
      <h3>Postal Code: $postal</h3>
      <br>

      <table>
      <tr>
      <td>Product Cost</td>
      <td>Shipping Cost</td>
      <td>Service Fee</td>
      <td>Total</td>
      </tr>
      <br>
      <tr>
      <td>$$productPrice<span>+</span></td>
      <td>$$amount<span>+</span></td>
      <td>$$serviceFee<span>=</span></td>
      <td>$$total </td>
      </tr>
      </table>";

       printSuccess($client, $response);
  }
  else
  {
       printError($client, $response);
  }

  writeToLog($client);    // Write to log file

} catch (SoapFault $exception) {
 printFault($exception, $client);
}

//FUNCTIONS
function addShipper(){
    $shipper = array(
        'Contact' => array(
            'PersonName' => 'XXX',
            'CompanyName' => 'XXX',
            'PhoneNumber' => 'XXX'),
        'Address' => array(
            'StreetLines' => array('Address Line 1'),
            'City' => 'New York',
            'StateOrProvinceCode' => 'XXX',
            'PostalCode' => 'XXXX',
            'CountryCode' => 'US')
    );
    return $shipper;
}
function addRecipient(){
    $country = $_POST['country'];
    $postal = $_POST['postal'];
    $recipient = array(
        'Contact' => array(
            'PersonName' => 'Recipient Name',
            'CompanyName' => 'Company Name',
            'PhoneNumber' => 'XXX'
        ),
        'Address' => array(
            'StreetLines' => array('Address Line 1'),
            'City' => '',
            'StateOrProvinceCode' => '',
            'PostalCode' =>  ''.$postal.'',
            'CountryCode' => ''.$country.'',
            'Residential' => false)
    );
    return $recipient;
}
//HANDLING CHARGE
function addHandlingCharge(){
    $VariableHandlingChargeDetail = array(
            'RateTypeBasis' => 'LIST',
            'RateElementBasis' => 'BASE_CHARGE', // valid values NET_CHARGE, NET_CHARGE_EXCLUDING_TAXES, NET_FREIGHT
            'FixedValue' => array('Currency' => 'USD', 'Amount' => 0),
            'PercentValue' =>  '0',
          );
         return $VariableHandlingChargeDetail;
};


function addShippingChargesPayment(){
    $shippingChargesPayment = array(
        'PaymentType' => 'SENDER', // valid values RECIPIENT, SENDER and THIRD_PARTY
        'Payor' => array(
            'ResponsibleParty' => array(
            'AccountNumber' => getProperty('billaccount'),
          //  'PostalCode' => '10036',
            'CountryCode' => 'US')
        )
    );
    return $shippingChargesPayment;
}


function addLabelSpecification(){
    $labelSpecification = array(
        'LabelFormatType' => 'COMMON2D', // valid values COMMON2D, LABEL_DATA_ONLY
        'ImageType' => 'PDF',  // valid values DPL, EPL2, PDF, ZPLII and PNG
        'LabelStockType' => 'PAPER_7X4.75');
    return $labelSpecification;
}
function addSpecialServices(){
    $specialServices = array(
        'SpecialServiceTypes' => array('COD'),
        'CodDetail' => array(
            'CodCollectionAmount' => array('Currency' => 'USD', 'Amount' => 150),
            'CollectionType' => 'ANY')// ANY, GUARANTEED_FUNDS
    );
    return $specialServices;
}
function addPackageLineItem1(){
    $packageLineItem = array(
        'SequenceNumber'=>1,
        'GroupPackageCount'=>1,
        'Weight' => array(
            'Value' => 2,
            'Units' => 'LB'
        ),
        'Dimensions' => array(
            'Length' => 2,
            'Width' => 2,
            'Height' => 2,
            'Units' => 'IN'
        )
    );
    return $packageLineItem;
   }
?>

quick-quote.php(模板文件包含特定页面上的功能):

<?php /* Template Name: quick-quote */ ?>

<?php
get_header();

include('RateService_v20_php/RateAvailableServicesService_v20_php/php/RateWebServiceClient/RateAvailableServices/RateAvailableServicesWebServiceClient.php');

get_footer();
?>

functions.php(仅限相关代码):

add_action( 'admin_post_quick_quote', 'prefix_admin_quick_quote' );
function prefix_admin_quick_quote() {
    // What do I do here!?
}

我遇到的问题是:
- 我填写表格
- 提交
-print_r($ _ POST)数据,以查看表格是否正确发布(确实如此)
- require_once('../../library/fedex-common.php');之后的任何代码都不执行

我已经检查/尝试过的事情:
-PHP和SOAP正确安装在服务器上 - 所有文件都是多余的664而不是666 -ran服务器上的文件,它工作正常 - 将表单的操作设置为“rate.php”或“quick-quote.php”(表单将数据发布到同一页面,因此我认为这可以留空) -made sure('../../library/fedex-common.php')是正确的路径

所以我的问题是:
- 如何在提交表单后执行php脚本?
- 这是使用functions.php挂钩admin-post.php吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

听起来PHP无法访问fedex-common.php。

  • 将&#39; require_once`的相对路径更改为绝对路径。
  • 检查应用程序是否具有该目录的权限。
  • 检查应用程序是否有权执行该文件。

此外,您可以通过替换&#39; require_once&#39;来检查包含是否成功。使用以下内容进一步确定问题是否实际上是文件位置的副作用或引用文件的问题。

if(!@include_once("/PATH/TO/library/fedex-common.php")) {
    throw new Exception ('fedex-common.php does not exist');
}