0

我尝试将 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 作为产品发布,它可以工作。

有什么建议么???

4

1 回答 1

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
]);
于 2016-05-13T17:20:09.663 回答