Magento-装运方法carrier.php
这将是一个有点长的机会,因为我真的不知道该问什么 我在app/code/local/ParcelMonkeyLMc中创建了一个定制的发货方法。我希望它从parcelmonkey.co.uk API中读取 我在一个单独的php文件中使用了API,但是当我将代码移到carrier.php时,它似乎不起作用 在公共功能中,我有以下代码:Magento-装运方法carrier.php,php,magento,Php,Magento,这将是一个有点长的机会,因为我真的不知道该问什么 我在app/code/local/ParcelMonkeyLMc中创建了一个定制的发货方法。我希望它从parcelmonkey.co.uk API中读取 我在一个单独的php文件中使用了API,但是当我将代码移到carrier.php时,它似乎不起作用 在公共功能中,我有以下代码: $quoterequest = array('parcelmonkey.request' => array(
$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);
$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';
pGetQuote以及其他与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';/*修改我*/
$password='password';/*从混合($requestphparray)修改我;
$dom->formatOutput=true;
$xmlrequestbody=$dom->saveXML();
//ASSERT:$xmlbody将PHP数组作为XML结构字符串包含。
// -----------------------------------------------------------------------------
//发送XML。
$xmlreply=pmSendXmlRequest($xmlrequestbody,$pmapi100url);
//ASSERT:$xmlreply包含XML应答。
//调试。
//echo$xmlreply.
;
// -----------------------------------------------------------------------------
//防御代码。
//检查一下,我们得到了一些XML作为回复。
if(substr($xmlreply,0,38)==
'')
{
//我们有XML。
// -------------------------------------------------------------------------
//将XML回复转换为PHP数组。
$replyphparray=toArray($xmlreply);
}
其他的
{
//错误-XML回复看起来不正确。
$replyphparray['replycode']=922;
$replyphparray['replymessage']=“内部错误:”。$xmlreply;
}
//ASSERT:$phpreplyarray包含作为PHP数组的XML应答。
// -----------------------------------------------------------------------------
//返回PHP数组。
返回$replyphparray;
}
// *********************************************************************************
//
//
//低级程序
//
//
// *********************************************************************************
//PHP数组到XML字符串。
// ~~~~~~~~~~~~~~~~~~~~~~~
//
//以下例程/类将PHP数组转换为XML结构化字符串。
//
// http://www.devexp.eu/2009/04/11/php-domdocument-convert-array-to-xml/
/**
*扩展DOMDocument以实现个人(实用工具)方法。
*
*@作者Toni Van de Voorde
*/
类XmlDomConstruct扩展了DOMDocument{
/**
*从数组或字符串构造元素和文本。
*数组可以在索引部分包含元素的名称
*以及值部分中的元素文本。
*
*它还可以在同一个位置上创建具有相同元素标记名的xml
*水平。
*
*例:
*
*正文
*
*你好
*世界
*
*
*
*然后,数组应该如下所示:
*
*排列(
*“节点”=>数组(
*“节点”=>数组(
*0=>“文本”
*1=>数组(
*“字段”=>数组(
*0=>“你好”
*1=>“世界”
* )
* )
* )
* )
* )
*
*@param mixed$混合了一个数组或字符串。
*
*@param-DOMElement[可选]$DOMElement-Then-element
*从阵列将被构造到的位置。
*
*/
来自混合的公共函数($mixed,DOMELENT$DOMELENT=null){
$domElement=is_null($domElement)?$this:$domElement;
if(是_数组($mixed)){
foreach($mixed as$index=>$mixedElement){
if(is_int($index)){
如果($index==0){
$node=$domElement;
}否则{
$node=$this->createElement($doElement->tagName);
$doElement->parentNode->appendChild($node);
}
}
否则{
$node=$this->createElement($index);
$doElement->appendChild($node);
}
$this->frommix($mixedElement,$node);
}
}否则{
$doElement->appendChild($this->createTextNode($mixed));
}
}
}
// *********************************************************************************
//XML字符串到PHP数组
// ~~~~~~~~~~~~~~~~~~~~~~~
//
//以下例程将XML字符串转换为PHP数组。
//
// http://snipplr.com/view/3491/convert-php-array-to-xml-or-simple-xml-object-if-you-wish/
函数toArray($xml){
如果(is_string($xml))$xml=new simplexmlement($xml);
$children=$xml->children();
if(!$children)返回(字符串)$xml;
$arr=array();
foreach($key=>$node的子节点){
$node=toArray($node);
//支持“anon”非关联数组
如果($key=='anon'),$key=count($arr);
//如果已经设置了节点,请将其放入数组中
if(isset($arr[$key])){
如果(!is_array($arr[$key])| |$arr[$key][0]==null)$arr[$key]=array($arr[$key]);
$arr[$key][]=$node;
}否则{
$arr[$key]=$node;
}
}
返回$arr;
}
// *********************************************************************************
函数pmSendXmlRequest($xml,$pmapi100url)
// ~