2017-03-14 6 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 ('인증 : 무기명 xxxx')); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, TRUE); $ res = curl_exec ($ curl); $ response = json_decode ($ res); print_r ($ response); –

+0

완벽 ... 작동 중입니다. – Gowri