Magento - 运输方式carrier.php

时间:2014-12-04 13:49:57

标签: php magento

这将是一个长镜头,因为我真的不知道该问什么。

我在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。我知道这很长,但我不确定你们需要什么细节。

我有没有理由突然得到一个零而不是一个值?

由于

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

编辑: 根据要求,我放在Carrier.php中的所有ParcelMonkey函数。我发现函数 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;
}

1 个答案:

答案 0 :(得分:0)

我已经想到了这一点,基本上是以下代码:

$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 功能中。

经过多次头部刮擦后这么简单的修复。