2016-05-13 5 views
0

MagentoでGuzzleHttpを使用して私の製品カタログを更新しようとしました。GuzzleHttpが私に500のエラーを与える

私は、データベースからデータを取得するには、プール要求を使用して、私はプールの要求がうまく動作

$apiUrl = 'http://xxx.xxxxx.xxx'; 

$middleware = new Oauth1([ 
    'consumer_key' => '-------------------------------', 
    'consumer_secret' => '-------------------------------', 
    'token'   => '-------------------------------', 
    'token_secret' => '-------------------------------' 
]); 
$stack->push($middleware); 


$client = new Client(); 

$clientUpdate = new Client([ 
    'base_uri' => $apiUrl, 
    'handler' => $stack, 
    'auth' => 'oauth' 
]); 

$mapper = new JsonMapper(); 



$requests = function ($total) { 
    $uri = 'http://10.0.0.114:15021/myservice'; 

    for ($i = 1; $i <= $total; $i++) { 
     yield new Request('GET', $uri.$i); 
    } 
}; 

$pool = new Pool($client, $requests(3), [ 
    'concurrency' => 3, 
    'fulfilled' => function ($response, $index) { 

     global $clientUpdate, $mapper; 

     $productsArray = $mapper->mapArray(
      json_decode($response->getBody($asString = TRUE)), new ArrayObject(), 'ProductGo' 
     ); 

     $product = new Product(); 
     $stock = new StockData(); 

     foreach($productsArray as &$value){ 

      $product->type_id = "simple"; 
      $product->attribute_set_id = 4; 
      $product->sku = $value->xxxxxxxxxxxxxxx; 
      $product->weight = 1; 
      $product->name = $value->xxxxxxxx; 
      $product->price = $value->xxxxxxxx; 
      $product->status = 1; 
      $product->visibility =4; 
      $product->tax_class_id = 4; 
      $product->description = $value->xxxxxxx; 
      $product->short_description = $value->xxxxxx; 

      $stock->qty = $value->xxxxx; 
      $stock->min_qty = "0"; 
      $stock->is_qty_decimal = 0; 
      $stock->is_in_stock = 1; 

      $product->stock_data = (object) array_filter((array) $stock); 

      $productIn = (object) array_filter((array) $product); 
      try{ 
       $response = $clientUpdate->request('POST', '/api/rest/products', ['json' => json_encode($productIn)]); 

       echo $response; 
      }catch (RequestException $e) { 
       echo GuzzleHttp\Psr7\str($e->getRequest()); 
       if ($e->hasResponse()) { 
        echo GuzzleHttp\Psr7\str($e->getResponse()); 
       } 
      } 

     } 
    }, 
    'rejected' => function ($reason, $index) { 
     echo $reason; 
    }, 
]); 

// Initiate the transfers and create a promise 
$promise = $pool->promise(); 

// Force the pool of requests to complete. 
$promise->wait(); 

MagentoのtroughtのAPIにそれらを投稿してみてください。私はデバッグする場合は、私が要求を行うところ私は、ブレークポイントを配置しますが、私は

PHP Fatal error: Uncaught exception 'GuzzleHttp\Exception\ServerException' with message 'Server error: 500' in /home/xxxxxxxxxxx/PhpstormProjects/xxxxxxxxxxx/vendor/guzzlehttp/guzzle/src/Middleware.php:68 
Stack trace: 
#0 /home/xxxxxxxxxxx/PhpstormProjects/xxxxxxxxxxx/vendor/guzzlehttp/promises/src/Promise.php(199): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) 
#1 /home/xxxxxxxxxxx/PhpstormProjects/xxxxxxxxxxx/vendor/guzzlehttp/promises/src/Promise.php(152): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array) 
#2 /home/xxxxxxxxxxx/PhpstormProjects/xxxxxxxxxxx/vendor/guzzlehttp/promises/src/TaskQueue.php(60): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}() 
#3 /home/xxxxxxxxxxx/PhpstormProjects/xxxxxxxxxxx/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(96): GuzzleHttp\Promise\TaskQueue->run() 
#4 /home/xxxxxxxxxxx/PhpstormProjects/xxxxxxxxxxx/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(123): GuzzleHttp\Handler\CurlMultiHandler->tick() 
#5 /home/xxxxxxxxxxx/PhpstormPr in /home/xxxxxxxxxxx/PhpstormProjects/xxxxxxxxxxx/vendor/guzzlehttp/guzzle/src/Middleware.php on line 68 

これまでと同じエラーを取得した後、私が問題だかを理解することはできません、私は製品として郵便配達で、このJSONを投稿しようとしましたそれは動作します。

提案がありますか?

+1

がつがつ食うが500エラーを受信して​​いるという事実は、致命的なエラーがAPIで発生したことを意味します。何が起きているのかを知るには、APIを実行しているサーバーのエラーログを確認する必要があります。 – Tchoupi

答えて

0

http://docs.guzzlephp.org/en/latest/request-options.html#jso

概要:

JSONオプションが容易リクエストのボディ としてJSONエンコードされたデータをアップロードするために使用されます。 Content-Typeヘッダーがメッセージに既に存在しない場合は、application/jsonのContent-Typeヘッダーが に追加されます。

ここではリクエストに応じてヘッダを設定する方法の例です:

$client->request('POST', $url, [ 
    'headers' => [ 
     'Accept'  => 'application/json', 
    ], 
    'body' = $body 
]); 
+1

'json'オプションを使用すると、Content-Typeが 'application/json'に設定されていない場合、自動的にContent-Typeが設定されます。だからここには本当に問題はない。 – Tchoupi