Skip to content

Commit

Permalink
- Add Method Convert Array To XML
Browse files Browse the repository at this point in the history
- Remove isset( $response['Head']['ErrorMessage'] because i need that result
- Add Method CreateProduct, GetResponse, MigrateImages, SetImages
  • Loading branch information
mabuak committed Jan 21, 2018
1 parent fa03776 commit 0011271
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 23 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);

```
98 changes: 80 additions & 18 deletions src/LazadaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Exception;
use GuzzleHttp\Client;
use Spatie\ArrayToXml\ArrayToXml;

class LazadaClient {

Expand Down Expand Up @@ -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
*
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}

0 comments on commit 0011271

Please sign in to comment.