GuzzleHttp给我500错误

时间:2016-05-13 16:42:42

标签: php magento guzzle guzzle6

我尝试使用GuzzleHttp和Magento来更新我的产品目录。

我使用池请求从数据库获取数据,我尝试将它们发布到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 个答案:

答案 0 :(得分:0)

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

要点:

  

json选项用于轻松上传JSON编码数据作为正文   请求。将添加application / json的Content-Type标头   如果消息中没有Content-Type标头。

以下是有关如何根据请求设置标头的示例:

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