double-break / spapi-php

Amazon Selling Partner API PHP Client
MIT License
56 stars 48 forks source link

error for the first run to get catalog or orders #49

Closed mimouni closed 2 years ago

mimouni commented 2 years ago

I install first the framework : composer require double-break/spapi-php making the update : composer update double-break/spapi-php

then create a file test.php :

<?php
  include __DIR__ . '/vendor/autoload.php';

  /** The Setup **/

  $config = [
    //Guzzle configuration
    'http' => [
      'verify' => false,    //<--- NOT SAFE FOR PRODUCTION
      'debug' => true       //<--- NOT SAFE FOR PRODUCTION
    ],

    //LWA: Keys needed to obtain access token from Login With Amazon Service
    'refresh_token' => '<YOUR-REFRESH-TOKEN>',
    'client_id' => '<CLINET-ID-IN-SELLER-CONSOLE>',
    'client_secret' => '<CLIENT_SECRET>',

    //STS: Keys of the IAM role which are needed to generate Secure Session
    // (a.k.a Secure token) for accessing and assuming the IAM role
    'access_key' => '<STS-ACCESS_KEY>',
    'secret_key' => '<STS-SECRET-KEY>',
    'role_arn' => '<ROLE-ARN>' ,

    //API: Actual configuration related to the SP API :)
    'region' => 'eu-west-1',
    'host' => 'sellingpartnerapi-eu.amazon.com'
  ];

  //Create token storage which will store the temporary tokens
  $tokenStorage = new DoubleBreak\Spapi\SimpleTokenStorage('./aws-tokens');

  //Create the request signer which will be automatically used to sign all of the
  //requests to the API
  $signer = new DoubleBreak\Spapi\Signer();

  //Create Credentials service and call getCredentials() to obtain
  //all the tokens needed under the hood

  $credentials = new DoubleBreak\Spapi\Credentials($tokenStorage, $signer, $config);
  $cred = $credentials->getCredentials();

  /** The application logic implementation **/

  //Create SP API Catalog client and execute one ot its REST methods.
  $catalogClient = new DoubleBreak\Spapi\Api\CatalogItems($cred, $config);

  //Check the catalog info for B074Z9QH5F ASIN
  $result = $catalogClient->getCatalogItem('B074Z9QH5F', [
    'MarketplaceId' => 'A1PA6795UKMFR9',
  ])['payload'];

  print_r($result);
?>

and this generate the error :


Warning: curl_setopt_array(): Cannot represent a stream of type Output as a STDIO FILE* in C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 59

Warning: curl_setopt_array(): Cannot represent a stream of type Output as a STDIO FILE* in C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 59

Warning: curl_setopt_array(): Cannot represent a stream of type Output as a STDIO FILE* in C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 59

Fatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 0: The cURL request was retried 3 times and did not succeed. The most likely reason for the failure is that cURL was unable to rewind the body of the request and subsequent retries resulted in the same error. Turn on the debug option to see what went wrong. See https://bugs.php.net/bug.php?id=47204 for more information. (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php:201 Stack trace: #0 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(537): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array) #1 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(152): GuzzleHttp\Handler\CurlFactory::retryFailedRewind(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Array) #2 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(105): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #3 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #4 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(542): GuzzleHttp\Handler\CurlHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array) #5 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(152): GuzzleHttp\Handler\CurlFactory::retryFailedRewind(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Array) #6 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(105): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #7 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #8 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(542): GuzzleHttp\Handler\CurlHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array) #9 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(152): GuzzleHttp\Handler\CurlFactory::retryFailedRewind(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Array) #10 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(105): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #11 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #12 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\Proxy.php(28): GuzzleHttp\Handler\CurlHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array) #13 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\Proxy.php(51): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler\{closure}(Object(GuzzleHttp\Psr7\Request), Array) #14 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\PrepareBodyMiddleware.php(66): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler\{closure}(Object(GuzzleHttp\Psr7\Request), Array) #15 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Middleware.php(29): GuzzleHttp\PrepareBodyMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array) #16 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\RedirectMiddleware.php(70): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Request), Array) #17 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Middleware.php(59): GuzzleHttp\RedirectMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array) #18 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\HandlerStack.php(71): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Request), Array) #19 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Client.php(351): GuzzleHttp\HandlerStack->__invoke(Object(GuzzleHttp\Psr7\Request), Array) #20 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Client.php(162): GuzzleHttp\Client->transfer(Object(GuzzleHttp\Psr7\Request), Array) #21 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Client.php(182): GuzzleHttp\Client->requestAsync('post', Object(GuzzleHttp\Psr7\Uri), Array) #22 C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Client.php(95): GuzzleHttp\Client->request('post', '/auth/o2/token', Array) #23 C:\local_games\spamazon12\vendor\double-break\spapi-php\src\Credentials.php(107): GuzzleHttp\Client->__call('post', Array) #24 C:\local_games\spamazon12\vendor\double-break\spapi-php\src\Credentials.php(33): DoubleBreak\Spapi\Credentials->getLWAToken() #25 C:\local_games\spamazon12\test12.php(41): DoubleBreak\Spapi\Credentials->getCredentials() #26 {main} thrown in C:\local_games\spamazon12\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 201
jookeer37 commented 2 years ago

I get the same error. Any suggestion ? Thanks

mimouni commented 2 years ago

it's a problem with curl.

I was testing with xampp as localserver, I changed it to another and the problem was solved.

on some cases first time you should create the an empty file : aws-tokens on the root folder.