簡體   English   中英

Magento-運送方式carrier.php

[英]Magento - Shipping Method carrier.php

這將是一個漫長的過程,因為我真的不知道該問些什么。

我已經在app / code / local / ParcelMonkeyLMc中創建了一個自定義送貨方式。 我希望它從parcelmonkey.co.uk API中讀取。

我的API在單獨的php文件中運行良好,但是當我將代碼移到carrier.php時,它似乎無法正常工作。

在公共函數collectRates內部,我有以下代碼:

                $quoterequest = array('parcelmonkey.request' => array(
                    'login' => array(
                        'version'       => $version,
                        'username'      => $username,
                        'password'      => $password,
                        'test'          => $testflag
                        ),
                    'requesttype' => 'quote',
                    'quote' => array(
                        'packages'  => array(
                            'noofpackages'  => 1,
                            'package1'  => array(
                                'length'        => 10,
                                'width'         => 10,
                                'height'        => 10,
                                'weight'        => 1.5
                                ),
                            ),
                        'insurance_cover'   => 0.00,
                        'collection' => array(
                            'address'   => array(
                                'postcode'  => 'PR7 1DB',
                                'country'   => 'United Kingdom'
                                )
                            ),
                        'delivery' => array(
                            'address'   => array(
                                'postcode'  => 'NW1 4RY',
                                'country'   => 'United Kingdom'
                                )
                            )
                        )
                    )
                );

      $quotereply=pmGetQuote($quoterequest, $pmapi100url);

pmGetQuote以及其他與parcelmonkey相關的功能都在頁面頂部。 如果有人需要知道確切的代碼,我可以將其發布。

$ quotereply是查詢parcelmonkey的結果。 看起來像這樣:

Array ( [replycode] => 200 [replymessage] => success [replytype] => quote [quote] => Array ( [services] => Array ( [noofservices] => 2 [service1] => Array ( [name] => TestService1Before12am [description] => Test Service 1 Before 12am [carrier] => Camel [price] => 10 [vat] => 0 [vat_rate] => 0 [insurance_cost] => 0 [insurance_cover] => 0 ) [service2] => Array ( [name] => TestService2Anytime [description] => Test Service 2 Anytime [carrier] => Pigeon [price] => 5 [vat] => 0 [vat_rate] => 0 [insurance_cost] => 0 [insurance_cover] => 0 ) ) ) [custom] => Array ( [data] => [orderid] => ) )

在單獨的php文件中,我可以像這樣抓取特定字段:

$num = $quotereply['quote']['services']['noofservices'];

但是,當我在carrier.php中嘗試此操作時,我得到一個空值。 我知道這是漫長的過程,但是我不確定你們需要什么細節。

我為什么突然得到null而不是值是有原因的嗎?

謝謝

========================

編輯:根據要求,所有的ParcelMonkey函數,我已經放在Carrier.php中。 我發現該函數sendandgetreply返回一個錯誤,“通信錯誤:響應時出錯”。 在獨立的PHP文件上不會發生這種情況。

$username       = 'username';   /* MODIFY ME */
$password       = 'password';   /* MODIFY ME <- note, also referred to as key. */

$version        = '100';    /* Protocol version. */

$testflag       = '1';      /* Test flag, 0=live, 1=test. MODIFY ME WHEN YOU GO LIVE */

$pmapi100url    = 'http://www.parcelmonkey.co.uk/api/pmapi100.php';

function pmGetQuote($requestphparray, $pmapi100url)
//       ~~~~~~~~~~
//
// getQuote.
//
// Call this routine with the collection and delivery addresses and it will return
// a list of available services from Parcel Monkey with costs.
//
// As input it takes a PHP array and it returns the reply in a PHP array.
//
{
// Send and get reply.
$replyphparray = sendandgetreply($requestphparray,
                                 $pmapi100url);

return $replyphparray;
}

// *********************************************************************************


// <+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+>
// <+>                                                                           <+>
// <+>                      Server Communication Routines                        <+>
// <+>                                                                           <+>
// <+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+>

// *********************************************************************************

function sendandgetreply($requestphparray, $pmapi100url)
//       ~~~~~~~~~~~~~~~
//
// This routine takes the PHP array, converts it to XML, sends the XML to the Parcel 
// Monkey listener, gets the XML reply, converts the XML reply to a PHP array and
// returns the PHP array.
//
{

// -----------------------------------------------------------------------------
// Convert PHP Array to XML.

$dom = new XmlDomConstruct('1.0', 'utf-8');
$dom->fromMixed($requestphparray);
$dom->formatOutput = true; 
$xmlrequestbody = $dom->saveXML();

// ASSERT: $xmlbody contains the PHP Array as an XML structued string.

// -----------------------------------------------------------------------------
// Send XML.

$xmlreply = pmSendXmlRequest($xmlrequestbody, $pmapi100url);

// ASSERT: $xmlreply contains the XML reply.

// Debug.
//echo $xmlreply.<br/>;

// -----------------------------------------------------------------------------
// Defensive code.

// Check we have got some XML back as a reply.
if (substr($xmlreply,0,38)==
    '<'.chr(63)/*?*/.'xml version="1.0" encoding="utf-8"'.chr(63)/*?*/.'>')
{
    // We have XML.

    // -------------------------------------------------------------------------
    // Convert XML reply to PHP Array.

    $replyphparray = toArray ($xmlreply);
}
else
{
    // Error - XML reply doesn't look right.

    $replyphparray['replycode'] = 922;
    $replyphparray['replymessage'] = 'internal error: '.$xmlreply;

}

// ASSERT: $phpreplyarray contains the XML reply as a PHP array.

// -----------------------------------------------------------------------------
// return PHP array.

return $replyphparray;
}

// *********************************************************************************

// <+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+>
// <+>                                                                           <+>
// <+>                          Low Level Routines                               <+>
// <+>                                                                           <+>
// <+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+><+>

// *********************************************************************************

// PHP Array to XML String.
// ~~~~~~~~~~~~~~~~~~~~~~~
//
// The following routine/class will convert a PHP Array to an XML Structured string.
//
// http://www.devexp.eu/2009/04/11/php-domdocument-convert-array-to-xml/

/**
* Extends the DOMDocument to implement personal (utility) methods.
*
* @author Toni Van de Voorde
*/
class XmlDomConstruct extends DOMDocument {

/**
 * Constructs elements and texts from an array or string.
 * The array can contain an element's name in the index part
 * and an element's text in the value part.
 *
 * It can also creates an xml with the same element tagName on the same
 * level.
 *
 * ex:
 * <nodes>
 *   <node>text</node>
 *   <node>
 *     <field>hello</field>
 *     <field>world</field>
 *   </node>
 * </nodes>
 *
 * Array should then look like:
 *
 * Array (
 *   "nodes" => Array (
 *     "node" => Array (
 *       0 => "text"
 *       1 => Array (
 *         "field" => Array (
 *           0 => "hello"
 *           1 => "world"
 *         )
 *       )
 *     )
 *   )
 * )
 *
 * @param mixed $mixed An array or string.
 *
 * @param DOMElement[optional] $domElement Then element
 * from where the array will be construct to.
 *
 */
public function fromMixed($mixed, DOMElement $domElement = null) {

    $domElement = is_null($domElement) ? $this : $domElement;

    if (is_array($mixed)) {
        foreach( $mixed as $index => $mixedElement ) {

            if ( is_int($index) ) {
                if ( $index == 0 ) {
                    $node = $domElement;
                } else {
                    $node = $this->createElement($domElement->tagName);
                    $domElement->parentNode->appendChild($node);
                }
            } 

            else {
                $node = $this->createElement($index);
                $domElement->appendChild($node);
            }

            $this->fromMixed($mixedElement, $node);

        }
    } else {
        $domElement->appendChild($this->createTextNode($mixed));
    }

}
}

// *********************************************************************************

// XML String to PHP Array
// ~~~~~~~~~~~~~~~~~~~~~~~
//
// The following routine will convert a XML string to a PHP array.
//
// http://snipplr.com/view/3491/convert-php-array-to-xml-or-simple-xml-object-if-you-wish/

function toArray( $xml ) {
    if ( is_string( $xml ) ) $xml = new SimpleXMLElement( $xml );
    $children = $xml->children();
    if ( !$children ) return (string) $xml;
    $arr = array();
    foreach ( $children as $key => $node ) {
        $node = toArray( $node );

        // support for 'anon' non-associative arrays
        if ( $key == 'anon' ) $key = count( $arr );

        // if the node is already set, put it into an array
        if ( isset( $arr[$key] ) ) {
            if ( !is_array( $arr[$key] ) || $arr[$key][0] == null ) $arr[$key] = array( $arr[$key] );
            $arr[$key][] = $node;
        } else {
            $arr[$key] = $node;
        }
    }
    return $arr;
}

// *********************************************************************************

function pmSendXmlRequest($xml, $pmapi100url)
//       ~~~~~~~~~~~~~~~~
//
// This routine will send $xml to the API server and return the reply.
// 
{
    // This is the URL to the Parcel Monkey API listener.
$url = $pmapi100url;

$params = array('http' => array(
  'method' => 'POST',
  'content' => http_build_query(
    array('xml' => $xml)
  )
));

$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if(!$fp) { $response="Communication error: ERROR on open"; }

$response = @stream_get_contents($fp);
if($response === false) { $response="Communication error: ERROR on response"; }

return $response;
}

我已經弄清楚了,基本上是下面的代碼:

$username       = 'username';   /* MODIFY ME */
$password       = 'password';   /* MODIFY ME <- note, also referred to as key. */
$version        = '100';    /* Protocol version. */
$testflag       = '1';      /* Test flag, 0=live, 1=test. MODIFY ME WHEN YOU GO LIVE */
$pmapi100url    = 'http://www.parcelmonkey.co.uk/api/pmapi100.php';

需要在collectRates函數內。

經過大量頭部抓傷之后,這種簡單的修復方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM