身份验证失败了FedEx PHP API的错误

时间:2016-09-23 09:57:34

标签: php api fedex

我正在尝试将我的系统与FedEx集成。我已经下载了PHP代码文件,获得了我的开发密钥并在文件中更新了它们。但是,当我尝试执行我收到身份验证错误。我现在已经摸不着头脑了一天但是无法修复它。我发布这个问题的原因是,所有这些不同的键我甚至不确定我是否使用了正确的细节。如果我使用正确的钥匙,那么任何人都可以帮助我吗?如果我是在我的请求中导致问题的原因。

以下是我的RateAvailableServicesWebServiceClient文件:

<?php
// Copyright 2009, FedEx Corporation. All rights reserved.
// Version 12.0.0

require_once('fedex-common.php');

//The WSDL is not included with the sample code.
//Please include and reference in $path_to_wsdl variable.
$path_to_wsdl = "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(
    'ParentCredential' => array(
        'Key' => getProperty('parentkey'),
        'Password' => getProperty('parentpassword')
    ),
    'UserCredential' => array(
        'Key' => getProperty('key'), 
        'Password' => getProperty('password')
    )
); 
$request['ClientDetail'] = array(
    'AccountNumber' => getProperty('shipaccount'), 
    'MeterNumber' => getProperty('meter')
);
$request['TransactionDetail'] = array('CustomerTransactionId' => ' *** Rate Available Services Request 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');
// Service Type and Packaging Type are not passed in the request
$request['RequestedShipment']['Shipper'] = array(
    'Address'=>getProperty('address1')
);
$request['RequestedShipment']['Recipient'] = array(
    'Address'=>getProperty('address2')
);
$request['RequestedShipment']['ShippingChargesPayment'] = array(
    'PaymentType' => 'SENDER',
    'Payor' => array(
        'ResponsibleParty' => array(
            'AccountNumber' => getProperty('billaccount'),
            'Contact' => null,
            'Address' => array(
                'CountryCode' => 'US'
            )
        )
    )
);                                                              
$request['RequestedShipment']['PackageCount'] = '2';
$request['RequestedShipment']['RequestedPackageLineItems'] = array(
    '0' => array(
        'SequenceNumber' => 1,
        'GroupPackageCount' => 1,
        'Weight' => array(
            'Value' => 2.0,
            'Units' => 'LB'
        ),
        'Dimensions' => array(
            'Length' => 10,
            'Width' => 10,
            'Height' => 3,
            'Units' => 'IN'
        )
    ),
    '1' => array(
        'SequenceNumber' => 2,
        'GroupPackageCount' => 1,
        'Weight' => array(
            'Value' => 5.0,
            'Units' => 'LB'
        ),
        'Dimensions' => array(
            'Length' => 20,
            'Width' => 20,
            'Height' => 10,
            'Units' => 'IN'
         )
    )
);



try {
    if(setEndpoint('changeEndpoint')){
        $newLocation = $client->__setLocation(setEndpoint('endpoint'));
    }

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

    //echo "<pre>";print_r($client);echo "</pre>";
    //echo "<pre>";print_r($response);echo "</pre>";
    //die;

    if ($response -> HighestSeverity != 'FAILURE' && $response -> HighestSeverity != 'ERROR'){
        echo 'Rates for following service type(s) were returned.'. Newline. Newline;
        echo '<table border="1">';
        echo '<tr><td>Service Type</td><td>Amount</td><td>Delivery Date</td>';
        if(is_array($response -> RateReplyDetails)){
            foreach ($response -> RateReplyDetails as $rateReply){
                printRateReplyDetails($rateReply);
            }
        }else{
            printRateReplyDetails($response -> RateReplyDetails);          
        }
        echo '</table>'. Newline;
        printSuccess($client, $response);
    }else{
        printError($client, $response); 
    } 

    writeToLog($client);    // Write to log file   
} catch (SoapFault $exception) {
   printFault($exception, $client);        
}



function printRateReplyDetails($rateReply){
    echo '<tr>';
    $serviceType = '<td>'.$rateReply -> ServiceType . '</td>';
    if($rateReply->RatedShipmentDetails && is_array($rateReply->RatedShipmentDetails)){
        $amount = '<td>$' . number_format($rateReply->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Amount,2,".",",") . '</td>';
    }elseif($rateReply->RatedShipmentDetails && ! is_array($rateReply->RatedShipmentDetails)){
        $amount = '<td>$' . number_format($rateReply->RatedShipmentDetails->ShipmentRateDetail->TotalNetCharge->Amount,2,".",",") . '</td>';
    }
    if(array_key_exists('DeliveryTimestamp',$rateReply)){
        $deliveryDate= '<td>' . $rateReply->DeliveryTimestamp . '</td>';
    }else{
        $deliveryDate= '<td>' . $rateReply->TransitTime . '</td>';
    }
    echo $serviceType . $amount. $deliveryDate;
    echo '</tr>';
}
?>

和fedex-common.php文件:

<?php
// Copyright 2009, FedEx Corporation. All rights reserved.

/**
 *  Print SOAP request and response
 */
define('Newline',"<br />");


function printSuccess($client, $response) {
    printReply($client, $response);
}

function printReply($client, $response){
    $highestSeverity=$response->HighestSeverity;
    if($highestSeverity=="SUCCESS"){echo '<h2>The transaction was successful.</h2>';}
    if($highestSeverity=="WARNING"){echo '<h2>The transaction returned a warning.</h2>';}
    if($highestSeverity=="ERROR"){echo '<h2>The transaction returned an Error.</h2>';}
    if($highestSeverity=="FAILURE"){echo '<h2>The transaction returned a Failure.</h2>';}
    echo "\n";
    printNotifications($response -> Notifications);
    printRequestResponse($client, $response);
}

function printRequestResponse($client){
    echo '<h2>Request</h2>' . "\n";
    echo '<pre>' . htmlspecialchars($client->__getLastRequest()). '</pre>';  
    echo "\n";

    echo '<h2>Response</h2>'. "\n";
    echo '<pre>' . htmlspecialchars($client->__getLastResponse()). '</pre>';
    echo "\n";
}

/**
 *  Print SOAP Fault
 */  
function printFault($exception, $client) {
   echo '<h2>Fault</h2>' . "<br>\n";                        
   echo "<b>Code:</b>{$exception->faultcode}<br>\n";
   echo "<b>String:</b>{$exception->faultstring}<br>\n";
   writeToLog($client);

  echo '<h2>Request</h2>' . "\n";
    echo '<pre>' . htmlspecialchars($client->__getLastRequest()). '</pre>';  
    echo "\n";
}

/**
 * SOAP request/response logging to a file
 */                                  
function writeToLog($client){  

  /**
     * __DIR__ refers to the directory path of the library file.
     * This location is not relative based on Include/Require.
     */
    if (!$logfile = fopen(__DIR__.'/fedextransactions.log', "a"))
    {
        error_func("Cannot open " . __DIR__.'/fedextransactions.log' . " file.\n", 0);
        exit(1);
    }
    fwrite($logfile, sprintf("\r%s:- %s",date("D M j G:i:s T Y"), $client->__getLastRequest(). "\r\n" . $client->__getLastResponse()."\r\n\r\n"));

}

/**
 * This section provides a convenient place to setup many commonly used variables
 * needed for the php sample code to function.
 */
function getProperty($var){

  if($var == 'key') Return 'ivfEYX8HZPgzdgsh'; 
    if($var == 'password') Return 'bDKw3F3ZRA214qZa4m0mwIPKT​'; 
    if($var == 'shipaccount') Return '510087283';
    if($var == 'billaccount') Return '510087283';
    if($var == 'dutyaccount') Return '510087283'; 
    if($var == 'freightaccount') Return '510087283';  
    if($var == 'trackaccount') Return '510087283'; 
    if($var == 'dutiesaccount') Return '510087283';
    if($var == 'importeraccount') Return '510087283';
    if($var == 'brokeraccount') Return '510087283';
    if($var == 'distributionaccount') Return '510087283';
    if($var == 'locationid') Return 'PLBA';
    if($var == 'printlabels') Return true;
    if($var == 'printdocuments') Return true;
    if($var == 'packagecount') Return '4';
    if($var == 'validateaccount') Return '510087283';
    if($var == 'meter') Return '510087283';

    if($var == 'shiptimestamp') Return mktime(10, 0, 0, date("m"), date("d")+1, date("Y"));

    if($var == 'spodshipdate') Return '2016-04-13';
    if($var == 'serviceshipdate') Return '2013-04-26';
  if($var == 'shipdate') Return '2016-04-21';

    if($var == 'readydate') Return '2014-12-15T08:44:07';
    //if($var == 'closedate') Return date("Y-m-d");
    if($var == 'closedate') Return '2016-04-18';
    if($var == 'pickupdate') Return date("Y-m-d", mktime(8, 0, 0, date("m")  , date("d")+1, date("Y")));
    if($var == 'pickuptimestamp') Return mktime(8, 0, 0, date("m")  , date("d")+1, date("Y"));
    if($var == 'pickuplocationid') Return 'SQLA';
    if($var == 'pickupconfirmationnumber') Return '1';

    if($var == 'dispatchdate') Return date("Y-m-d", mktime(8, 0, 0, date("m")  , date("d")+1, date("Y")));
    if($var == 'dispatchlocationid') Return 'NQAA';
    if($var == 'dispatchconfirmationnumber') Return '4';        

    if($var == 'tag_readytimestamp') Return mktime(10, 0, 0, date("m"), date("d")+1, date("Y"));
    if($var == 'tag_latesttimestamp') Return mktime(20, 0, 0, date("m"), date("d")+1, date("Y"));   

    if($var == 'expirationdate') Return date("Y-m-d", mktime(8, 0, 0, date("m"), date("d")+15, date("Y")));
    if($var == 'begindate') Return '2014-10-16';
    if($var == 'enddate') Return '2014-10-16';  

    if($var == 'trackingnumber') Return 'XXX';

    if($var == 'hubid') Return '5531';

    if($var == 'jobid') Return 'XXX';

    if($var == 'searchlocationphonenumber') Return '5555555555';
    if($var == 'customerreference') Return '39589';

    if($var == 'shipper') Return array(
        'Contact' => array(
            'PersonName' => 'Manager of Business Support',
            'CompanyName' => 'Guardian CSC',
            'PhoneNumber' => '717-848-2540'
        ),
        'Address' => array(
            'StreetLines' => array('6000 Susquehanna Plaza Drive'),
            'City' => 'York',
            'StateOrProvinceCode' => 'PA',
            'PostalCode' => '17406',
            'CountryCode' => 'US',
            'Residential' => 1
        )
    );
    if($var == 'recipient') Return array(
        'Contact' => array(
            'PersonName' => 'Harry Bale',
            'CompanyName' => 'Harry & COs',
            'PhoneNumber' => '1234567890'
        ),
        'Address' => array(
            'StreetLines' => array('Address Line 1'),
            'City' => 'LA',
            'StateOrProvinceCode' => 'CA',
            'PostalCode' => '90001',
            'CountryCode' => 'US',
            'Residential' => 1
        )
    );  

    if($var == 'address1') Return array(
        'StreetLines' => array('10 Fed Ex Pkwy'),
        'City' => 'Memphis',
        'StateOrProvinceCode' => 'TN',
        'PostalCode' => '38115',
        'CountryCode' => 'US'
    );
    if($var == 'address2') Return array(
        'StreetLines' => array('13450 Farmcrest Ct'),
        'City' => 'Herndon',
        'StateOrProvinceCode' => 'VA',
        'PostalCode' => '20171',
        'CountryCode' => 'US'
    );                    
    if($var == 'searchlocationsaddress') Return array(
        'StreetLines'=> array('240 Central Park S'),
        'City'=>'Austin',
        'StateOrProvinceCode'=>'TX',
        'PostalCode'=>'78701',
        'CountryCode'=>'US'
    );

    if($var == 'shippingchargespayment') Return array(
        'PaymentType' => 'SENDER',
        'Payor' => array(
            'ResponsibleParty' => array(
                'AccountNumber' => getProperty('billaccount'),
                'Contact' => null,
                'Address' => array('CountryCode' => 'US')
            )
        )
    );  
    if($var == 'freightbilling') Return array(
        'Contact'=>array(
            'ContactId' => 'freight1',
            'PersonName' => 'Big Shipper',
            'Title' => 'Manager',
            'CompanyName' => 'Freight Shipper Co',
            'PhoneNumber' => '1234567890'
        ),
        'Address'=>array(
            'StreetLines'=>array(
                '1202 Chalet Ln', 
                'Do Not Delete - Test Account'
            ),
            'City' =>'Harrison',
            'StateOrProvinceCode' => 'AR',
            'PostalCode' => '72601-6353',
            'CountryCode' => 'US'
            )
    );
}

function setEndpoint($var){
    if($var == 'changeEndpoint') Return false;
    if($var == 'endpoint') Return 'https://wsbeta.fedex.com/web-services';
}

function printNotifications($notes){
    foreach($notes as $noteKey => $note){
        if(is_string($note)){    
            echo $noteKey . ': ' . $note . Newline;
        }
        else{
            printNotifications($note);
        }
    }
    echo Newline;
}

function printError($client, $response){
    printReply($client, $response);
}

function trackDetails($details, $spacer){
    foreach($details as $key => $value){
        if(is_array($value) || is_object($value)){
            $newSpacer = $spacer. '&nbsp;&nbsp;&nbsp;&nbsp;';
            echo '<tr><td>'. $spacer . $key.'</td><td>&nbsp;</td></tr>';
            trackDetails($value, $newSpacer);
        }elseif(empty($value)){
            echo '<tr><td>'.$spacer. $key .'</td><td>'.$value.'</td></tr>';
        }else{
            echo '<tr><td>'.$spacer. $key .'</td><td>'.$value.'</td></tr>';
        }
    }
}
?>

我发布有问题凭据的原因是我将此帐户仅用于测试目的,并且在我上线时将更改其他帐户。 以下是联邦快递提供的密钥:

开发人员测试密钥:ivfEYX8HZPgzdgsh

测试帐号:510087283

测试仪表编号:118748019

测试FedEx Office Integrator ID:123

测试客户端产品ID:TEST

测试客户端产品版本:9999

测试密码:bDKw3F3ZRA214qZa4m0mwIPKT

2 个答案:

答案 0 :(得分:0)

任何时候您都会收到身份验证失败,因为您的凭据不正确或未经过适当的API使用认证。

FedEx要求其所有客户在创建生产标签之前完成认证流程。以下是说明:http://images.fedex.com/ca_english/businesstools/webservices/Web_Services_Guide_ENG.pdf

答案 1 :(得分:0)

您的仪表编号错误。你的代码:

if($var == 'meter') Return '510087283';

但是,以下是您的测试仪表编号:

  

118748019