diff --git a/README.md b/README.md index fee2295..ad49afb 100644 --- a/README.md +++ b/README.md @@ -21,18 +21,21 @@ $lazada = new ramadhan\LazadaClient( [ ``` ### Get products -Use this call to get all or a range of products. - -To get the parameters, you can check this link -https://lazada-sellercenter.readme.io/docs/getproducts - ```php +// Use this call to get all or a range of products. // $parameters = array('Limit' => 20, 'Offset' => 0) $lazada->GetProducts($parameters = array()); ``` +### Create product +```php +// Use this call to create products. +$lazada->CreateProduct($xmlContent); +``` + ### Available methods View source for detailed argument description. +All methods starting with an uppercase character are also documented in the [Lazada API documentation](https://lazada-sellercenter.readme.io/) ```php // Use this call to retrieve all product brands in the lazada system. @@ -45,4 +48,14 @@ $lazada->GetCategoryTree(); // It will also display attributes for TaxClass, with their possible values listed as options. // primaryCategory is Identifier of the category for which the caller wants the list of attributes. $lazada->GetCategoryAttributes($primaryCategory); + +//Use this call to get the returned information from the system for the UploadImages and MigrateImages API. +$lazada->GetResponse($requestId); + +//Use this call to migrate multiple images from an external site to Lazada site +$lazada->MigrateImages($imagesUrl); + +//Use this call to set the images for an existing product by associating one or more image URLs with it +$lazada->SetImages($xmlContent); + ``` \ No newline at end of file diff --git a/src/LazadaClient.php b/src/LazadaClient.php index d96b35b..5148c2d 100644 --- a/src/LazadaClient.php +++ b/src/LazadaClient.php @@ -9,6 +9,7 @@ use Exception; use GuzzleHttp\Client; +use Spatie\ArrayToXml\ArrayToXml; class LazadaClient { @@ -152,6 +153,18 @@ private function xmlToArray( $xmlstring ) { return json_decode( json_encode( simplexml_load_string( $xmlstring ) ), true ); } + /** + * Convert an array to xml + * + * @param $array array to convert + * @param $customRoot [$customRoot = 'AmazonEnvelope'] + * + * @return sting + */ + private function arrayToXml( array $array, $customRoot = 'Request' ) { + return ArrayToXml::convert( $array, $customRoot ); + } + /** * A method to Get A Brands * @@ -164,11 +177,7 @@ private function xmlToArray( $xmlstring ) { public function GetBrands( $limit = 100, $offset = 0 ) { $response = $this->request( array( 'Action' => 'GetBrands', 'Limit' => $limit, 'Offset' => $offset ), 'GET' ); - if ( isset( $response['Head']['ErrorMessage'] ) ) { - throw new Exception( $response['Head']['ErrorMessage'] ); - } - - return $response['Body']; + return $response; } /** @@ -180,11 +189,7 @@ public function GetBrands( $limit = 100, $offset = 0 ) { public function GetCategoryTree() { $response = $this->request( array( 'Action' => 'GetCategoryTree' ), 'GET' ); - if ( isset( $response['Head']['ErrorMessage'] ) ) { - throw new Exception( $response['Head']['ErrorMessage'] ); - } - - return $response['Body']; + return $response; } /** @@ -198,11 +203,7 @@ public function GetCategoryTree() { public function GetCategoryAttributes( $primaryCategory ) { $response = $this->request( array( 'Action' => 'GetCategoryAttributes', 'PrimaryCategory' => $primaryCategory ), 'GET' ); - if ( isset( $response['Head']['ErrorMessage'] ) ) { - throw new Exception( $response['Head']['ErrorMessage'] ); - } - - return $response['Body']; + return $response; } /** @@ -229,11 +230,72 @@ public function GetProducts( $parameters = [] ) { $response = $this->request( $requestParameter, 'GET' ); - if ( isset( $response['Head']['ErrorMessage'] ) ) { - throw new Exception( $response['Head']['ErrorMessage'] ); + return $response; + } + + /** + * A method to Create Products + * + * @param $xmlContent + * + * @return mixed + */ + public function CreateProduct( $xmlContent ) { + $response = $this->request( array( 'Action' => 'CreateProduct' ), 'POST', $xmlContent ); + + return $response; + } + + /** + * A method to Get Response + * + * @param $requestId + * + * @return array|mixed|string + */ + public function GetResponse( $requestId ) { + $xmlContent = $this->arrayToXml( [ 'RequestId' => $requestId ] ); + + $response = $this->request( array( 'Action' => 'GetResponse' ), 'POST', $xmlContent ); + + return $response; + } + + /** + * A method to Migrate Images + * + * @param array $imagesUrl + * + * @return mixed + * @throws Exception + */ + public function MigrateImages( $imagesUrl = array() ) { + + if ( count( $imagesUrl ) > 8 ) { + throw new Exception( 'Maximum amount of Image Urls\'s for this call is 8' ); + } + + foreach ( $imagesUrl as $imageUrl ) { + $images['Url'][] = $imageUrl; } - return $response['Body']; + $xmlContent = $this->arrayToXml( [ 'Images' => $images ] ); + + $response = $this->request( array( 'Action' => 'MigrateImages' ), 'POST', $xmlContent ); + + return $response; } + /** + * A method to Set Images + * + * @param $xmlContent + * + * @return array|mixed|string + */ + public function SetImages( $xmlContent ) { + $response = $this->request( array( 'Action' => 'SetImages' ), 'POST', $xmlContent ); + + return $response; + } } \ No newline at end of file