我正在编写一个亚马逊MWS脚本,需要包含多个PHP文件并运行这些文件中包含的函数。
每个函数都可以从各自的PHP文件中运行,但只运行第一个函数。第二个函数,无论我使用什么顺序,都会对我传递的参数抛出致命错误。
致命错误:未捕获的异常'异常'消息'无法根据提供的数据构建。请务必通过关联数组或DOMElement'
以下是我尝试运行的前两个函数:
// Initialize and run ListInboundShipments.
require_once 'ListInboundShipments.php';
// Set status array and timeframe criteria for filtering shipments
$shipmentStatusList = new FBAInboundServiceMWS_Model_ShipmentStatusList();
$shipmentStatusList->setMember(array('WORKING','SHIPPED','IN_TRANSIT','DELIVERED','CHECKED_IN','RECEIVING','CLOSED','CANCELLED','DELETED','ERROR'));
$updatedAfter = date('Y-m-d', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
$updatedBefore = date('Y-m-d');
// Pass filter criteria into $request array
$request->setShipmentStatusList($shipmentStatusList);
$request->setLastUpdatedAfter($updatedAfter);
$request->setLastUpdatedBefore($updatedBefore);
// Create XML document of shipments.
$shipmentXML = invokeListInboundShipments($service, $request);
// Parse the new XML document.
$shipments = new SimpleXMLElement($shipmentXML);
$shipmentArray = array();
foreach ($shipments->ListInboundShipmentsResult->ShipmentData->member as $member) {
// Create array of all shipments.
$shipmentArray[] = array(
"ShipmentId"=>$member->ShipmentId,
"ShipmentStatus"=>$member->ShipmentStatus
);
}
// Destroy variables to get a clean slate.
unset($service); unset($request);
// Save token and run through ListInboundShipmentsByNextToken until
// it does not return a token.
$token = (string)$shipments->ListInboundShipmentsResult->NextToken;
while ($token != null) {
require_once 'ListInboundShipmentsByNextToken.php';
$request->setNextToken($token);
echo print_r($request);
// Create new XML document.
$shipmentXML = invokeListInboundShipmentsByNextToken($service, $request);
$shipments = new SimpleXMLElement($shipmentXML);
// Use XML document to create array of all shipments.
foreach ($shipments->ListInboundShipmentsResult->ShipmentData->member as $member) {
$shipmentArray[] = array(
"ShipmentId"=>$member->ShipmentId,
"ShipmentStatus"=>$member->ShipmentStatus
);
}
$token = $shipments->ListInboundShipmentsResult->NextToken;
}
echo print_r($request)
语句向我显示$request
确实是一个关联数组。我想也许错误是由冲突的变量引起的(每个函数都使用$ request和$ service),所以我尝试在调用它们之间使用unset()
。
答案 0 :(得分:1)
请勿在{{1}}循环中使用require_once
。它只在第一次循环时执行文件。第二次,它意识到文件已经执行,因此它不会再次执行。请改用while
。
require
对于需要重复执行的代码使用包含文件,这是不寻常的设计。通常,您只需在包含文件中定义函数,或使用它们初始化全局变量(例如数据库参数)。所以通常的方法是在include文件中定义一个函数,然后每次通过循环调用该函数,而不是将文件包含在循环中。