2017-03-14 1 views
0

httpリクエスト経由でcommercetools apiから削除しようとしています。続きcommercetools apiから製品を削除できません

が私のコードです:サーバーから

$url = 'https://api.sphere.io/test/products/xxxx'; 
$params = json_encode(array('version'=>1)); 

$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $params); 
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json')); 
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); 
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Bearer xxxx')); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 
$res = curl_exec($curl); 
$response = json_decode($res); 

print_r($response); 

は応答:

stdClass Object ([statusCode] => 400 [message] => Missing version number [errors] => Array ([0] => stdClass Object ([code] => InvalidOperation [message] => Missing version number))) 

私はのparamsにバージョン番号を送信し、それでもエラーを取得しています。助けてください

+0

となります。 – Gowri

答えて

2

私の最初のアドバイスは、commercetools PHP SDKを使用することです。 https://github.com/commercetools/commercetools-php-sdkここで見つけるか、SDKと製品の削除作曲https://packagist.org/packages/commercetools/php-sdk

を使用している場合は、次のようになります

<?php 

namespace Commercetools\Core; 

use Commercetools\Core\Request\Products\ProductDeleteRequest; 

require __DIR__ . '/../vendor/autoload.php'; 


// create the api client config object 
$config = Config::fromArray([ 
    'client_id' => 'XXX', 
    'client_secret' => 'XXX', 
    'scope' => 'xxxx' 
]); 

$request = ProductDeleteRequest::ofIdAndVersion('123456', 1); 

$client = Client::ofConfig($config); 

$response = $client->execute($request); 
$deletedProduct = $request->mapFromResponse($response); 

すると、あなたが本当に直接APIに話に固執したい場合は、クエリのようにバージョンを送信する必要がありますパラメータは、http://dev.commercetools.com/http-api-projects-products.html#delete-product-by-idのドキュメントに記載されています。あなたの例のURLは

$url = 'https://api.sphere.io/test/products/xxxx?version=1'; 
+0

私はこれを行う必要がありますhttp apiを使用して、任意のSDKを使用したくないです。 – Gowri

+0

だからこれが行く方法です: $ url = 'https://api.sphere.io/test/products/xxxx?version=1'; $ curl = curl_init($ url); curl_setopt($ curl、CURLOPT_HTTPHEADER、array( 'Accept:application/json')); curl_setopt($ curl、CURLOPT_CUSTOMREQUEST、 "DELETE"); curl_setopt($ curl、CURLOPT_HTTPHEADER、array( 'Authorization:Bearer xxxx')); curl_setopt($ curl、CURLOPT_RETURNTRANSFER、TRUE); $ res = curl_exec($ curl); $ response = json_decode($ res); print_r($ response); –

+0

完璧です...うまくいきます。 – Gowri

関連する問題