2016-05-13 74 views
0

我嘗試使用GuageHttp和Magento來更新我的產品目錄。GuzzleHttp給我500錯誤

我用池請求以從數據庫中的數據,我試圖把它們上傳到Magento的trought API

$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(); 

池要求工作正常。如果我調試我將提出一個破發點,我提出一個要求,但之後我得到過同樣的錯誤

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的內容類型頭部 。

下面是關於如何設置首標上請求的示例:

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

這意味着當使用'json'選項時,如果Content-Type沒有被設置,它將自動將Content-Type設置爲'application/json'。所以這裏真的沒有問題。 – Tchoupi