简体   繁体   中英

Amazon MWS (PHP) - Report Request API functions return without data, no error thrown

I am currently working with the Amazon MWS to integrate some features into wordpress via a plugin. I am using the client libraries provided by amazon found here:

https://developer.amazonservices.com/api.html?group=bde&section=reports&version=latest

Using these client libraries and the sample php files included I have set up my plugin to make two API calls. The first is requestReport

public function requestInventoryReport() {
    AWI_Amazon_Config::defineCredentials(); // Defines data for API Call
    $serviceUrl = "https://mws.amazonservices.com";
    $config = array (
            'ServiceURL' => $serviceUrl,
            'ProxyHost' => null,
            'ProxyPort' => -1,
            'MaxErrorRetry' => 3,
    );
    $service = new MarketplaceWebService_Client(
            AWS_ACCESS_KEY_ID,
            AWS_SECRET_ACCESS_KEY,
            $config,
            APPLICATION_NAME,
            APPLICATION_VERSION);
    $request = new MarketplaceWebService_Model_RequestReportRequest();
    $request->setMerchant(MERCHANT_ID);
    $request->setReportType('_GET_MERCHANT_LISTINGS_DATA_');
    self::invokeRequestReport($service, $request);
}

private function invokeRequestReport(MarketplaceWebService_Interface $service, $request) {
    try {
        $response = $service->requestReport($request);
        if ($response->isSetRequestReportResult()) {
            // Print Out Data
        }
    } catch (MarketplaceWebService_Exception $ex) {
        // Print Out Error
    }
}

and the second is getReportRequestList which has code similar to the first function. I am able to run these functions without any errors. The issue that I am having is that $response->isSetRequestReportResult() returns false. From my understanding and looking into the response object, this would suggest that the response object does not have the result. (Upon printing out the response object I can see that the FieldValue of the result array is NULL.) The call, however, does not throw an error but neither does it have the result.

I did some digging through the code and found that the result does actually get returned from the api call but never gets set to the return object when the library attempts to parse it from XML. I've tracked the error down to this block of code (This code is untouched by me and directly from the amazon mws reports library).

private function fromDOMElement(DOMElement $dom)
{
    $xpath = new DOMXPath($dom->ownerDocument);
    $xpath->registerNamespace('a', 'http://mws.amazonaws.com/doc/2009-01-01/');

    foreach ($this->fields as $fieldName => $field) {
        $fieldType = $field['FieldType'];   
        if (is_array($fieldType)) {
            if ($this->isComplexType($fieldType[0])) {
                // Handle Data
            } else {
                // Handle Data 
            }
        } else {
            if ($this->isComplexType($fieldType)) {
                // Handle Data   
            } else {
                $element = $xpath->query("./a:$fieldName/text()", $dom);
                $data = null;
                if ($element->length == 1) {
                    switch($this->fields[$fieldName]['FieldType']) {
                        case 'DateTime':
                            $data = new DateTime($element->item(0)->data, 
                                new DateTimeZone('UTC'));
                            break;
                        case 'bool':
                            $value = $element->item(0)->data;
                            $data = $value === 'true' ? true : false;
                            break;
                        default:
                            $data = $element->item(0)->data;
                            break;
                    }
                  $this->fields[$fieldName]['FieldValue'] = $data;
                }
            }
        }
    }
}

The data that should go into the RequestReportResult exists at the beginning of this function as a node in the dom element. The flow of logic takes it into the last else statement inside the foreach. The code runs its query and returns $element however $element->length = 13 in my case which causes it to fail the if statement and never set the data to the object. I have also looked into $element->item(0) to see what was in it and it appears to be a dom object itself matching the original dom object but with a bunch of empty strings.

Now, I'm new to working with the MWS and my gut feeling is that I am missing a parameter somewhere in my api call that is messing up how the data is returned and is causing this weird error, but I'm out of ideas at this point. If anyone has any ideas or could point me in the right direction, I would greatly appreciate it.

Thanks for your time!

** Also as a side note, Amazon Scratchpad does return everything properly using the same parameters that I am using in my code **

These works for me, check if you are missing anything. For RequestReportRequest i am doing this:

$request = new MarketplaceWebService_Model_RequestReportRequest();
$marketplaceIdArray = array("Id" => array($pos_data['marketplace_id']));
$request->setMarketplaceIdList($marketplaceIdArray);
$request->setMerchant($pos_data['merchant_id']);
$request->setReportType($this->report_type);

For GetReportRequestList i am doing this:

$service = new MarketplaceWebService_Client($pos_data['aws_access_key'], $pos_data['aws_secret_access_key'], $pos_data['config'], $pos_data['application_name'], $pos_data['application_version']);
$report_request = new MarketplaceWebService_Model_GetReportRequestListRequest();
$report_request->setMerchant($pos_data["merchant_id"]);
$report_type_request = new MarketplaceWebService_Model_TypeList();
$report_type_request->setType($this->report_type);
$report_request->setReportTypeList($report_type_request);
$report_request_status = $this->invokeGetReportRequestList($service, $report_request, $report_requestID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM