2014-12-04 35 views
0

这将是一个远射,因为我真的不知道该问什么。Magento - 运输方式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我得到一个空。我知道这很长时间了,但我不确定你们需要什么细节。

是否有原因让我突然变得空而不是价值?

感谢

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

编辑: 按照要求,所有的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; 
} 
+0

您可以发布pmGetQuote函数的确切代码吗?我的猜测是Magento所做的一件事就是搞砸了。 – 2014-12-07 17:19:46

+0

我已经添加了所有的ParcelMonkey功能,我希望这有助于!谢谢! – Lee 2014-12-08 11:09:00

回答

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函数中。

这样一个简单的修复后,大量的头部划痕。

相关问题