0

My goal is to have the user fill out a form with country and postal code, post the data to a php script that makes a call to fedex's server and returns shipment rates.

I have successfully accomplished this on my local web server, but upon implementation to my WordPress site, the php script does not execute when the form data is posted. My code is as follows:

rate.php (form and php script both in this file):

<!-- 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 (template file to include feature on specific pages):

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

<?php
get_header();

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

get_footer();
?>

functions.php (relevant code only):

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

The issue that I have having is this:
-I fill out the form
-hit submit
-print_r($_POST) the data to see if form posted correctly (it does)
-any code after require_once('../../library/fedex-common.php'); does not execute

Things I have already checked/tried:
-PHP and SOAP are installed correctly on the server
-all files are excessible 664 not 666
-ran the file on the server, it works fine
-setting the form's action to "rate.php" or "quick-quote.php" (form posts data to same page so I think this can be left blank)
-made sure ('../../library/fedex-common.php') is the correct path

So my questions are:
-How do I make the php script execute after the form is submitted?
-Is this accomplished using functions.php to hook into admin-post.php?

Thanks so much!

1 Answer 1

1

It sounds like PHP does not have access to fedex-common.php.

  • Change the relative path for the 'require_once` to an absolute path.
  • Check that the application has permissions to the directory.
  • Check that the application has permissions to execute the file.

Additionally, you can check that the include is success by replacing the 'require_once' with the following to further determine if the issue actually a side effect of the file location or an issue with the referenced file.

if(!@include_once("/PATH/TO/library/fedex-common.php")) {
    throw new Exception ('fedex-common.php does not exist');
}
Sign up to request clarification or add additional context in comments.

4 Comments

Ok I tried: mysite/wp-content/themes/mytheme/RateService_v20_php/… with no luck. That file is 664 I checked. Thanks!
The absolute path should look more like /PATH/TO/library/fedex-common.php.
Sorry the link is cut off because of it's length, if you mouse over it you should be able to see it in its entirety. Does it look about right?
The path you supplied is a URL. An absolute path is the local system path to the file. The directory structure would similar to the output of echo getcwd() . "\n";.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.