caponica / AmazonMwsComplete

Name-spaced wrappers for the full set of Amazon MWS client libraries
56 stars 53 forks source link

Parameter must be an array or an object that implements Countable #20

Closed bizou78 closed 6 years ago

bizou78 commented 6 years ago

Hello Caponica,

First, thank you for your great work on this Bundle, it help me a lot.

At the moment i have no problem on the first part of my work (Get better price of products ), but now i start to try push a product on Amazon with callSubmitFeed() and i have this error "Parameter must be an array or an object that implements Countable".

You can show my code below: `$mwsClientPoolUsa = new MwsClientPool(); $mwsClientPoolUsa->setConfig([ 'amazon_site' => MwsClientPoolConfig::SITE_FRANCE, 'access_key' => 'my_access_key', 'secret_key' => 'my_secret_key', 'application_name' => 'testAmazon', 'application_version' => '1.01', 'seller_id' => 'my_seller_id', ]);

    $productClientPackUsa = $mwsClientPoolUsa->getFeedAndReportClientPack();

$feed = <<<EOD

<?xml version="1.0" encoding="iso-8859-1"?>

1.01 xxxxxxxxxx
Product false 1 Update 9782013224055 R+ POCHE JEUNESSE 3 EAN 9782013224055 G_GEN_NOTAX Example Product Title Example Product Brand This is an example product description. Example Bullet Point 1 Example Bullet Point 2 100.00 Example Product Manufacturer example-item-type Example Ingredients Example Directions

EOD;

    $feedHandle = @fopen('php://temp', 'rw+');
    fwrite($feedHandle, $feed);
    rewind($feedHandle);

    $productClientPackUsa->callSubmitFeed('_POST_PRODUCT_DATA_', $feedHandle);

    @fclose($feedHandle);`

Can you help me a little bit ? Thank you.

caponica commented 6 years ago

I think I had that error before... but I can't remember how to fix it :-/

Have you tried with a real file (saved on disk) instead of a temp file?

The basic method you're using looks reasonable. My code looks similar:

    $clientPack = $mwsClientPool->getFeedAndReportClientPack();
    $fileHandle = @fopen($fullFilePath, 'r');
    /** @var \MarketplaceWebService_Model_SubmitFeedResponse $mwsResponse */
    $mwsResponse = $clientPack->callSubmitFeed($feedType, $fileHandle);
bizou78 commented 6 years ago

Thank you for your fast response, i'm gonna try your solution as soon as possible and i come back for tell you if it's working.

Thank you very much.

bizou78 commented 6 years ago

Hi,

I just test your solution and i have the same issue with a real file save on Disk.

you can show my code below:

`$fullPAth = '/var/www/testAmazon/document/report.txt'; var_dump($fullPAth);

    $feedHandle = @fopen($fullPAth, 'r');

    var_dump($feedHandle);
    fwrite($feedHandle, $feed);

    $productClientPackUsa->callSubmitFeed('_POST_PRODUCT_DATA_', $feedHandle);
    rewind($feedHandle);

    @fclose($feedHandle);`

and the var_dump($feedhandle): resource(573, stream)

I don't know what is going wrong here.

Thank you for your help.

bizou78 commented 6 years ago

I try to follow the code and i think i find something in /var/www/testAmazon/vendor/caponica/amazon-mws-complete/src/AmazonPhpClientLibrary/MarketplaceWebService/Client.php

var_dump($headers);
array_push($headers, "Content-Type: " . $request->getContentType()->toString());
      var_dump($headers);

The firsts var_dump() give me a response

/var/www/testAmazon/vendor/caponica/amazon-mws-complete/src/AmazonPhpClientLibrary/MarketplaceWebService/Client.php:1454: array (size=0) empty

But the second return nothing.

Do you think the problem is here ?

bizou78 commented 6 years ago

Finaly i find the problem, it's from ContentType.php

public function isSetParameters() { return count ($this->fields['Parameters']['FieldValue']) > 0; } the function call the function count but $this->fields['Parameters']['fieldValue'] is not an array

bizou78 commented 6 years ago

So i change the function by public function isSetParameters() { if (is_array($this->fields['Parameters']['FieldValue'])){ return count ($this->fields['Parameters']['FieldValue']) > 0; }else{ return false; } }

and it's work for me.

Do you think it's good ?

caponica commented 6 years ago

I'll try to take a look at this later today and get back to you

caponica commented 6 years ago

OK, so this is caused by your PHP version handling count() differently to PHP<7.2.0.

Changing the Amazon library is not the best idea, since it will be over-written when the library pack is next updated. (This is obviously frustrating, since some of the code in there is... interesting.)

If you're using PHP 7.2.x I guess you're likely to run into several compatibility issues until Amazon updates the codebase.

Based on the documentation, I'm guessing that you are getting a warning (rather than a fatal error) - is that correct? If so then you may be better using a custom error handler to deal with it rather than changing the Amazon library.

See discussions here and here for more insight, as well as the PHP docs here.

bizou78 commented 6 years ago

Sorry for my absence and thank you for your answer.

At the moment, i just change my php version for the 7.1 and it's work very well.

I will wait for use the 7.2 :)

Thank you for you help.