'; //echo $startpos.'

'; //echo $endpos.'

'; //echo $endofstring.'

'; //echo substr($url, 0, $startpos).'

'; //echo substr($url, $endpos, strlen($url) - $endpos).'

'; $url = substr( $url, 0, $startpos ) . $trackingId . substr( $endofstring, $endpos, strlen( $endofstring ) - $endpos ); //echo $url; //exit; //$url = str_replace('&AssociateTag='.$associateTag, '&AssociateTag='.$trackingId, $url); //$url = str_replace('&tag='.$associateTag, '&tag='.$trackingId, $url); //$url = str_replace('?tag='.$associateTag, '?tag='.$trackingId, $url); return $url; } public function check_configuration( $post_id ) { try { $apikey = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_key' ); $apisecretkey = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_secret_key' ); $website = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_website' ); $trackingid = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_tracking_id' ); $usessl = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_tracking_id' ); $message = ''; if ( $apikey != '' && $apisecretkey != '' ) { require_once ATKP_PLUGIN_DIR . '/lib/vendor/autoload.php'; $client = new \GuzzleHttp\Client(); $request = new \ApaiIO\Request\GuzzleRequest( $client ); $request->setScheme( $usessl ? 'https' : 'http' ); $conf = new \ApaiIO\Configuration\GenericConfiguration(); $conf ->setCountry( $website ) ->setAccessKey( $apikey ) ->setSecretKey( $apisecretkey ) ->setAssociateTag( $trackingid ) //->setRequest('\ApaiIO\Request\Rest\Request') //->setResponseTransformer('\ApaiIO\ResponseTransformer\XmlToDomDocument'); //->setRequest('\ApaiIO\Request\Soap\Request') //->setResponseTransformer('\ApaiIO\ResponseTransformer\ObjectToArray'); ->setRequest( $request ); $api = new \ApaiIO\ApaiIO( $conf ); $search = new ApaiIO\Operations\Search(); $search->setKeywords( 'Harry Potter' ); try { $test = $api->runOperation( $search ); } catch ( GuzzleHttp\Exception\ClientException $e ) { $response = $e->getResponse(); if ( isset( $response ) ) { $test = $response->getBody(); } else { $test = $e->getMessage(); } //error contains: Website Temporarily Unavailable //You are submitting requests too quickly. Please retry your requests at a slower rate. For more information, see Efficiency Guidelines. } if ( ATKPLog::$logenabled ) { ATKPLog::LogDebug( '*** check_configuration amazon ***' ); ATKPLog::LogDebug( serialize( $test ) ); ATKPLog::LogDebug( '*** check_configuration amazon ***' ); } $test = $this->convert_response( $test ); // Success if ( ! empty ( $test->OperationRequest->RequestId ) ) { //alles ok $subshopsold = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_default_shops' ); $subshops = array(); //add subshop for amazon $subshop = new subshop(); $subshop->title = __( 'Amazon', ATKP_PLUGIN_PREFIX ); $subshop->shopid = $post_id; $website = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_website' ); switch ( $website ) { case 'de': $subshop->logourl = plugins_url( 'images/logo-normal-amazon-de.jpg', ATKP_PLUGIN_FILE ); $subshop->smalllogourl = plugins_url( 'images/logo-small-amazon-de.jpg', ATKP_PLUGIN_FILE ); break; case 'com': $subshop->logourl = plugins_url( 'images/logo-normal-amazon-com.png', ATKP_PLUGIN_FILE ); $subshop->smalllogourl = plugins_url( 'images/logo-small-amazon-com.png', ATKP_PLUGIN_FILE ); break; } $subshop->enabled = true; array_push( $subshops, $subshop ); //für bestehende alte subshops ist dieser teil noch drinnen if ( is_array( $subshopsold ) ) { foreach ( $subshopsold as $shopold ) { if ( $subshop->shopid == $shopold->shopid && $subshop->programid == $shopold->programid ) { $subshop->enabled = $shopold->enabled; $subshop->customtitle = $shopold->customtitle; $subshop->customsmalllogourl = $shopold->customsmalllogourl; $subshop->customlogourl = $shopold->customlogourl; $subshop->customfield1 = $shopold->customfield1; $subshop->customfield2 = $shopold->customfield2; $subshop->customfield3 = $shopold->customfield3; } } } ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_default_shops', $subshops ); // Error Code } elseif ( ! empty ( $test->Error->Code ) ) { return $test->Error->Message; // Fallback for any other issue } else { return 'undefined'; } } else { //wenn zugangscodes gelöscht werden muss message auch geleert werden $message = 'Credientials are empty'; } return $message; } catch (GuzzleHttp\Exception\ServerException $exception) { $response = $exception->getResponse(); if($response != null && $response->getBody() != nulL) $responseBodyAsString = $response->getBody()->getContents(); else $responseBodyAsString = $exception->getMessage(); return 'Request ServerException: ' . $responseBodyAsString; } catch ( Exception $e ) { if ( ATKPLog::$logenabled ) { ATKPLog::LogError( $e->getMessage() ); } return $e->getMessage(); } } private function convert_response( $response ) { //return json_decode(json_encode($response), false); return json_decode( json_encode( (array) simplexml_load_string( $response ) ), 0 ); } public function set_configuration( $post_id ) { ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_amz_medium_image_size', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_medium_image_size', 'int' ) ); ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_amz_small_image_size', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_small_image_size', 'int' ) ); ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_website', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_access_website', 'string' ) ); ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_tracking_id', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_access_tracking_id', 'string' ) ); ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_load_customer_reviews', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_load_customer_reviews', 'bool' ) ); ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_enable_ssl', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_enable_ssl', 'bool' ) ); ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_key', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_access_key', 'string' ) ); ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_secret_key', ATKPTools::get_post_parameter( ATKP_SHOP_POSTTYPE . '_amz_access_secret_key', 'string' ) ); //$selectedstore = ATKPTools::get_post_parameter(ATKP_PRODUCT_POSTTYPE.'_amz_store', 'int'); //if($selectedstore == '') // $selectedstore = 1; //ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE.'_amz_store', $selectedstore); //$subshops= ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE.'_default_shops'); //if($subshops == null || count($subshops) > 1) // $subshops = $this->get_defaultshops($post_id); //if(is_array($subshops)) // $this->read_subshop_settings($subshops, 'amz'); //ATKPTools::set_post_setting( $post_id, ATKP_SHOP_POSTTYPE.'_default_shops', $subshops); } private function get_defaultshops( $post_id ) { $subshops = array(); $website = ATKPTools::get_post_setting( $post_id, ATKP_SHOP_POSTTYPE . '_access_website' ); $subshop = new subshop(); $subshop->title = __( 'Amazon', ATKP_PLUGIN_PREFIX ); switch ( $website ) { case 'de': $subshop->logourl = plugins_url( 'images/logo-normal-amazon-de.jpg', ATKP_PLUGIN_FILE ); $subshop->smalllogourl = plugins_url( 'images/logo-small-amazon-de.jpg', ATKP_PLUGIN_FILE ); break; case 'com': $subshop->logourl = plugins_url( 'images/logo-normal-amazon-com.png', ATKP_PLUGIN_FILE ); $subshop->smalllogourl = plugins_url( 'images/logo-small-amazon-com.png', ATKP_PLUGIN_FILE ); break; } $subshop->shopid = $post_id; $subshop->programid = ''; $subshop->enabled = true; array_push( $subshops, $subshop ); return $subshops; } public function get_configuration( $post ) { $webservice = ATKPTools::get_post_setting( $post->ID, ATKP_SHOP_POSTTYPE . '_access_webservice' ); $apikey = ''; $apisecretkey = ''; $subshops = null; if ( $webservice == '1' ) { $apikey = ATKPTools::get_post_setting( $post->ID, ATKP_SHOP_POSTTYPE . '_access_key' ); $apisecretkey = ATKPTools::get_post_setting( $post->ID, ATKP_SHOP_POSTTYPE . '_access_secret_key' ); //$subshops= ATKPTools::get_post_setting($post->ID, ATKP_SHOP_POSTTYPE.'_default_shops'); //if($subshops == null || count($subshops) > 1) // $subshops = $this->get_defaultshops($post->ID); } ?> ID, ATKP_SHOP_POSTTYPE . '_enable_ssl' ), true ); ?>> ID, ATKP_SHOP_POSTTYPE . '_load_customer_reviews' ), true ); ?>> 1 ) { $subshops = $this->get_defaultshops( $post_id ); } foreach ( $subshops as $subshop ) { $subshop->shopid = $post_id; $subshop->programid = ''; $subshop->logourl = $subshop->customlogourl == '' ? $subshop->logourl : $subshop->customlogourl; $subshop->smalllogourl = $subshop->customsmalllogourl == '' ? $subshop->smalllogourl : $subshop->customsmalllogourl; $subshop->title = $subshop->customtitle == '' ? $subshop->title : $subshop->customtitle; $subshop->enabled = true; } return $subshops; } private $helper = null; private $enable_ssl = false; private $storetype = 1; private $country = ''; private $load_customer_reviews = false; private $associateTag = ''; private $accessKey = ''; private $shopid = ''; private $smallimagesize = 0; private $mediumimagesize = 0; public function checklogon( $shop ) { $this->shopid = $shop->id; $this->accessKey = $access_key = ATKPTools::get_post_setting( $shop->settingid, ATKP_SHOP_POSTTYPE . '_access_key' ); $access_secret_key = ATKPTools::get_post_setting( $shop->settingid, ATKP_SHOP_POSTTYPE . '_access_secret_key' ); $access_message = ATKPTools::get_post_setting( $shop->settingid, ATKP_SHOP_POSTTYPE . '_access_message' ); $this->country = $access_website = ATKPTools::get_post_setting( $shop->settingid, ATKP_SHOP_POSTTYPE . '_access_website' ); $this->associateTag = $access_tracking_id = ATKPTools::get_post_setting( $shop->settingid, ATKP_SHOP_POSTTYPE . '_access_tracking_id' ); $this->load_customer_reviews = ATKPTools::get_post_setting( $shop->settingid, ATKP_SHOP_POSTTYPE . '_load_customer_reviews' ); $this->enable_ssl = ATKPTools::get_post_setting( $shop->settingid, ATKP_SHOP_POSTTYPE . '_enable_ssl' ); $this->storetype = ATKPTools::get_post_setting( $shop->settingid, ATKP_SHOP_POSTTYPE . '_amz_store' ); $this->smallimagesize = ATKPTools::get_post_setting( $shop->settingid, ATKP_SHOP_POSTTYPE . '_amz_small_image_size' ); $this->mediumimagesize = ATKPTools::get_post_setting( $shop->settingid, ATKP_SHOP_POSTTYPE . '_amz_medium_image_size' ); if ( $access_tracking_id == '' ) { $access_tracking_id = 'empty'; } require_once ATKP_PLUGIN_DIR . '/lib/vendor/autoload.php'; $client = new \GuzzleHttp\Client(); $request = new \ApaiIO\Request\GuzzleRequest( $client ); $request->setScheme( $this->enable_ssl ? 'https' : 'http' ); $conf = new \ApaiIO\Configuration\GenericConfiguration(); $conf ->setCountry( $access_website ) ->setAccessKey( $access_key ) ->setSecretKey( $access_secret_key ) ->setAssociateTag( $access_tracking_id ) ->setRequest( $request ); $this->helper = new \ApaiIO\ApaiIO( $conf ); } public function retrieve_browsenodes( $keyword ) { if ( $this->helper == null ) { throw new Exception( 'checklogon required' ); } $nodes = array(); $search = new ApaiIO\Operations\Search(); $search->setPage( 1 ); $search->setCategory( 'All' ); //$search->setCondition('New'); $search->setKeywords( $keyword ); $search->setResponseGroup( array( 'BrowseNodes' ) ); switch ( $this->storetype ) { default: case 1: //amazon //$search->setMerchantId('Amazon'); break; case 2: //marketplace //TODO: break; case 3: //amazon marketplace break; } $pagination = 0; do { $pagination ++; //increment $search->setPage( $pagination ); try { $response = $this->helper->runOperation( $search ); $response = $this->convert_response( $response ); $check = $this->checkResponse( $response ); } catch ( GuzzleHttp\Exception\ClientException $e ) { $response = $e->getResponse(); if ( isset( $response ) ) { $check = $response->getBody(); } else { $check = $e->getMessage(); } } catch ( Exception $e ) { $check = $e->getMessage(); } //falls fehler aufgetreten ist, 2. versuch if ( ! empty( $check ) ) { sleep( 3 ); $response = $this->helper->runOperation( $search ); $response = $this->convert_response( $response ); $check = $this->checkResponse( $response ); } if ( ! empty( $check ) ) { throw new Exception( $check ); } if ( isset( $response->Items->Item ) ) { foreach ( $response->Items->Item as $result ) { if ( isset( $result->BrowseNodes->BrowseNode ) ) { if ( isset( $result->BrowseNodes->BrowseNode->Name ) ) { $nodes[ $result->BrowseNodes->BrowseNode->BrowseNodeId ] = $result->BrowseNodes->BrowseNode->Name; } foreach ( $result->BrowseNodes->BrowseNode as $browsenode ) { foreach ( $this->RecursiveBrowseNodes( $browsenode ) as $node => $value ) { $nodes[ $node ] = $value; } } } } } } while ( $pagination <= 2 ); $newNodes = array(); foreach ( $nodes as $node => $value ) { if ( ! array_key_exists( $node, $newNodes ) ) { $newNodes[ $node ] = $value; } } return $newNodes; } private function RecursiveBrowseNodes( $parentBrowseNode ) { $nodes = array(); if ( isset( $parentBrowseNode->Ancestors ) ) { foreach ( $parentBrowseNode->Ancestors as $browsenode ) { if ( ! isset( $browsenode->Name ) || ! is_string( $browsenode->Name ) ) { continue; } $nodes[ $browsenode->BrowseNodeId ] = $browsenode->Name; foreach ( $this->RecursiveBrowseNodes( $browsenode ) as $node => $value ) { $nodes[ $node ] = $value; } //array_push($nodes, $this->RecursiveBrowseNodes($browsenode)); } } return $nodes; } public function quick_search( $keyword, $searchType, $pagination = 1 ) { if ( $this->helper == null ) { throw new Exception( 'checklogon required' ); } $maxCount = 10; $products = array(); if ( $searchType == 'asin' || $searchType == 'ean' || $searchType == 'articlenumber' ) { $search = new ApaiIO\Operations\Lookup(); $search->setCategory( 'All' ); if ( $searchType == 'ean' ) { $search->setIdType( 'EAN' ); } $search->setItemId( explode( ',', $keyword ) ); } else { $search = new ApaiIO\Operations\Search(); $search->setCategory( 'All' ); //$search->setCondition('New'); $search->setKeywords( $keyword ); } $search->setResponseGroup( array( 'ItemAttributes', 'Images', 'OfferFull', 'Variations' ) ); //var_dump($search);exit; do { $search->setPage( $pagination ); try { $response = $this->helper->runOperation( $search ); $response = $this->convert_response( $response ); $check = $this->checkResponse( $response ); } catch ( GuzzleHttp\Exception\ClientException $e ) { $response = $e->getResponse(); if ( isset( $response ) ) { $check = $response->getBody(); } else { $check = $e->getMessage(); } } catch ( Exception $e ) { $check = $e->getMessage(); } //falls fehler aufgetreten ist, 2. versuch if ( ! empty( $check ) ) { sleep( 3 ); $response = $this->helper->runOperation( $search ); $response = $this->convert_response( $response ); $check = $this->checkResponse( $response ); } if ( ! empty( $check ) ) { throw new Exception( $check ); } if ( isset( $response->Items->Item ) ) { if ( ! is_array( $response->Items->Item ) ) { $response->Items->Item = array( $response->Items->Item ); } foreach ( $response->Items->Item as $result ) { if ( isset( $result->ASIN ) ) { $product = array(); if ( isset( $result->SmallImage->URL ) ) { $product['imageurl'] = $this->checkimageurl( $result->SmallImage->URL, 'small' ); } $product['articlenumber'] = $product['asin'] = $result->ASIN; if ( isset( $result->ItemAttributes->EAN ) ) { $product['ean'] = $result->ItemAttributes->EAN; } $product['producturl'] = $result->DetailPageURL; $product['title'] = htmlspecialchars( $result->ItemAttributes->Title ); if ( isset( $result->Offers->Offer->OfferListing->Availability ) ) { $product['availability'] = $result->Offers->Offer->OfferListing->Availability; } //TODO: lowest new price liefert völlig sinnfreie Preise aus dem marketplace zurück if ( isset( $result->OfferSummary->LowestNewPrice ) ) { $product['price'] = $result->OfferSummary->LowestNewPrice->FormattedPrice; } //$product['availability'] = $result->Offers->Offer->OfferListing->Availability; array_push( $products, $product ); } if ( count( $products ) >= $maxCount ) { break; } } } if ( count( $products ) >= $maxCount ) { break; } $pagination ++; //increment } while ( ( $pagination * 10 ) < $maxCount ); return $products; } private function checkurl( $url, $enable_ssl = null ) { if ( $enable_ssl == null ) { $enable_ssl = $this->enable_ssl; } if ( $enable_ssl ) { $url = str_replace( 'http://', 'https://', $url ); } return $url; } private function checkimageurl( $url, $size ) { if ( $this->enable_ssl ) { $url = str_replace( 'http://ecx.images-amazon.com', 'https://images-na.ssl-images-amazon.com', $url ); } if ( $size == 'small' && $this->smallimagesize > 0 ) { $url = str_replace( 'SL75', 'SL' . $this->smallimagesize, $url ); } if ( $size == 'medium' && $this->mediumimagesize > 0 ) { $url = str_replace( 'SL160', 'SL' . $this->mediumimagesize, $url ); } return $url; } private function checkResponse( $response ) { $requestHelp = null; if ( isset( $response->BrowseNodes->Request ) ) { $requestHelp = $response->BrowseNodes->Request; } else if ( isset( $response->Items->Request ) ) { $requestHelp = $response->Items->Request; } //echo('$response: ' .serialize($response)); $message = ''; if ( isset( $requestHelp->IsValid ) && $requestHelp->IsValid != 'True' ) { $message .= 'Invalid Request. IsValid: ' . $requestHelp->IsValid; //echo('xx '.serialize($requestHelp->Errors->Error)); } if ( isset( $requestHelp->Errors->Error ) ) { if ( isset( $requestHelp->Errors->Error->Code ) && $requestHelp->Errors->Error->Code != '' ) { $error = $requestHelp->Errors->Error; if ( $message != '' ) { $message .= ' '; } $message .= 'ErrorCode: ' . $error->Code; if ( $message != '' ) { $message .= ' '; } $message .= 'Message: ' . $error->Message; } else { foreach ( $requestHelp->Errors->Error as $error ) { if ( $message != '' ) { $message .= ' '; } $message .= 'ErrorCode: ' . $error->Code; if ( $message != '' ) { $message .= ' '; } $message .= 'Message: ' . $error->Message; } } } return $message; } public function retrieve_departments() { if ( $this->helper == null ) { throw new Exception( 'checklogon required' ); } $departments = array(); switch ( $this->country ) { case 'de': require_once ATKP_PLUGIN_DIR . '/includes/helper/atkp_amazon_helper_de.php'; $helper = new atkp_amazon_helper_de(); $departments = $helper->get_departments(); break; default: case 'en': require_once ATKP_PLUGIN_DIR . '/includes/helper/atkp_amazon_helper_en.php'; $helper = new atkp_amazon_helper_en(); $departments = $helper->get_departments(); break; } return $departments; } public function retrieve_filters() { $durations = array( '' => __( 'Not selected', ATKP_PLUGIN_PREFIX ), 'Keywords' => __( 'Keywords', ATKP_PLUGIN_PREFIX ), 'SearchIndex' => __( 'SearchIndex', ATKP_PLUGIN_PREFIX ), 'Sort' => __( 'Sort', ATKP_PLUGIN_PREFIX ), 'BrowseNode' => __( 'BrowseNode', ATKP_PLUGIN_PREFIX ), 'Availability' => __( 'Availability', ATKP_PLUGIN_PREFIX ), 'Power' => __( 'Power', ATKP_PLUGIN_PREFIX ), 'Actor' => __( 'Actor', ATKP_PLUGIN_PREFIX ), 'Artist' => __( 'Artist', ATKP_PLUGIN_PREFIX ), 'Author' => __( 'Author', ATKP_PLUGIN_PREFIX ), 'Brand' => __( 'Brand', ATKP_PLUGIN_PREFIX ), 'Composer' => __( 'Composer', ATKP_PLUGIN_PREFIX ), 'Conductor' => __( 'Conductor', ATKP_PLUGIN_PREFIX ), 'Director' => __( 'Director', ATKP_PLUGIN_PREFIX ), 'Conductor' => __( 'Conductor', ATKP_PLUGIN_PREFIX ), 'Manufacturer' => __( 'Manufacturer', ATKP_PLUGIN_PREFIX ), 'MaximumPrice' => __( 'Maximum price', ATKP_PLUGIN_PREFIX ), 'MinimumPrice' => __( 'Minimum price', ATKP_PLUGIN_PREFIX ), 'MinPercentageOff' => __( 'Min percentage off', ATKP_PLUGIN_PREFIX ), 'Orchestra' => __( 'Orchestra', ATKP_PLUGIN_PREFIX ), 'Publisher' => __( 'Publisher', ATKP_PLUGIN_PREFIX ), 'Condition' => __( 'Condition', ATKP_PLUGIN_PREFIX ), 'MerchantId' => __( 'Merchant Id', ATKP_PLUGIN_PREFIX ), ); return $durations; } public function retrieve_product( $asin, $id_type = 'ASIN' ) { if ( $this->helper == null ) { throw new Exception( 'checklogon required' ); } $products = $this->retrieve_products_internal( array( $asin ), $id_type ); if ( count( $products ) > 0 ) { return $products[0]; } else { return null; } } public function retrieve_products( $asins, $id_type = 'ASIN' ) { return $this->retrieve_products_internal( $asins, $id_type ); } public function retrieve_products_internal( $asins, $id_type ) { $atkpresponse = new atkp_response(); if ( count( $asins ) == 0 ) { return $atkpresponse; } switch ( $id_type ) { case 'TITLE': foreach ( $asins as $title ) { $search = new ApaiIO\Operations\Search(); $search->setCategory( 'All' ); $search->setResponseGroup( array( 'Large', 'Variations', 'OfferFull', 'Variations' ) ); $search->setKeywords( $title ); try { $response = $this->helper->runOperation( $search ); $response = $this->convert_response( $response ); $titlecheck = $this->checkResponse( $response ); } catch ( GuzzleHttp\Exception\ClientException $e ) { $response = $e->getResponse(); if ( isset( $response ) ) { $titlecheck = $response->getBody(); } else { $titlecheck = $e->getMessage(); } } catch ( Exception $e ) { $titlecheck = $e->getMessage(); } $added = false; if ( ! empty( $titlecheck ) || ! isset( $response->Items->Item ) ) { $responseitem = new atkp_response_item(); $responseitem->errormessage = empty( $titlecheck ) ? 'product not found' : $titlecheck; $responseitem->uniqueid = $title; $responseitem->uniquetype = $id_type; array_push( $atkpresponse->responseitems, $responseitem ); $added = true; } else { if ( is_array( $response->Items->Item ) ) { foreach ( $response->Items->Item as $result2 ) { if ( ! isset( $result2->ASIN ) || $added ) { continue; } $result = $result2; break; } } else { $result = $response->Items->Item; } $responseitem = new atkp_response_item(); $responseitem->productitem = $this->fill_product( $result ); $responseitem->uniqueid = $title; $responseitem->uniquetype = $id_type; array_push( $atkpresponse->responseitems, $responseitem ); $added = true; } if ( ! $added ) { $product = new atkp_response_item(); $product->errormessage = 'product not found'; $product->uniqueid = $title; $product->uniquetype = $id_type; array_push( $atkpresponse->responseitems, $product ); } sleep( 1 ); } break; case 'ARTICLENUMBER': case 'ASIN': case "EAN": $search = new ApaiIO\Operations\Lookup(); $search->setPage( 1 ); $search->setCategory( 'All' ); $search->setResponseGroup( array( 'Large', 'Variations', 'OfferFull', 'Variations' ) ); if ( $id_type == 'EAN' ) { $search->setIdType( ApaiIO\Operations\Lookup::TYPE_EAN ); } $search->setItemId( $asins ); try { $response = $this->helper->runOperation( $search ); $response = $this->convert_response( $response ); } catch (GuzzleHttp\Exception\ServerException $exception) { $response = $exception->getResponse(); if($response != null && $response->getBody() != nulL) $responseBodyAsString = $response->getBody()->getContents(); else $responseBodyAsString = $exception->getMessage(); $check = 'Request ServerException: ' . $responseBodyAsString; } catch ( GuzzleHttp\Exception\ClientException $e ) { $response = $e->getResponse(); if ( isset( $response ) ) { $check = $response->getBody(); } else { $check = $e->getMessage(); } } catch ( Exception $e ) { $check = $e->getMessage(); } if ( ! empty( $check ) ) { $atkpresponse->errormessage = $check; return $atkpresponse; } if ( isset( $response->Items->Request->Errors->Error ) ) { foreach ( $response->Items->Request->Errors->Error as $error ) { foreach ( $asins as $asin ) { $emessage = isset( $error->Message ) ? $error->Message : (string) $error; if ( stripos( $emessage, $asin ) !== false ) { $product = new atkp_response_item(); $product->errormessage = $emessage; $product->uniqueid = $asin; $product->uniquetype = $id_type; array_push( $atkpresponse->responseitems, $product ); } } } } $results = array(); if ( isset( $response->Items->Item ) ) { if ( is_array( $response->Items->Item ) ) { foreach ( $response->Items->Item as $result2 ) { array_push( $results, $result2 ); } } else { array_push( $results, $response->Items->Item ); } } foreach ( $results as $result ) { if ( ! isset( $result->ASIN ) ) { continue; } $product = new atkp_response_item(); $product->productitem = $this->fill_product( $result ); $product->uniquetype = $id_type; switch ( $id_type ) { case 'ASIN': case 'ARTICLENUMBER': $product->uniqueid = $this->findinput( $asins, $product->productitem->asin ); break; case 'EAN': $product->uniqueid = $this->findinput( $asins, $product->productitem->ean ); break; default: throw new exception( 'unknown id_type:' . $id_type ); } array_push( $atkpresponse->responseitems, $product ); } break; default: throw new Exception( 'unknown id_type: ' . $id_type ); break; } return $atkpresponse; } protected static function price_to_float( $s ) { $s = str_replace( ',', '.', $s ); // remove everything except numbers and dot "." $s = preg_replace( "/[^0-9\.]/", "", $s ); // remove all seperators from first part and keep the end $s = str_replace( '.', '', substr( $s, 0, - 3 ) ) . substr( $s, - 3 ); // return float return round( (float) $s, 2 ); } private function find_variation_product( $item ) { // Variations found! if ( isset( $item->Variations ) ) { if ( ATKPLog::$logenabled ) { ATKPLog::LogDebug( '*** variation check ***' ); ATKPLog::LogDebug( serialize( $item->Variations ) ); ATKPLog::LogDebug( '*** variation check ***' ); } foreach ( $item->Variations->Item as $varationItem ) { $newasin = $varationItem->ASIN; if ( ! empty( $newasin ) ) { if ( ATKPLog::$logenabled ) { ATKPLog::LogDebug( '*** is variation product: ' . $newasin ); } $variationProduct = $this->fill_product( $varationItem, $item ); if ( $variationProduct->salepricefloat != 0 ) { return $variationProduct; } sleep( 3 ); } } } return null; } private function fill_product( $result, $parentResult = null ) { /* if ( $parentResult == null ) { $found = $this->find_variation_product( $result ); if ( $found != null ) { $found->asin = $result->ASIN; if ( isset( $result->ItemAttributes->EAN ) ) { $found->ean = $result->ItemAttributes->EAN; } return $found; } } if ( ATKPLog::$logenabled ) { ATKPLog::LogDebug( '*** fill_product amazon ***' ); ATKPLog::LogDebug( serialize( $result ) ); ATKPLog::LogDebug( '*** fill_product amazon ***' ); } */ $myproduct = new atkp_product(); $myproduct->updatedon = current_time( 'timestamp' ); $myproduct->shopid = $this->shopid; //store the ASIN code in case we need it $myproduct->asin = $result->ASIN; $myproduct->parentasin = $result->ParentASIN; $variations = array(); $dimmension = array(); if(isset($result->Variations->Item)) { $vars = array(); if(is_array($result->Variations->Item)) $vars = $result->Variations->Item; else array_push($vars, $result->Variations->Item); if(is_array($result->Variations->VariationDimensions->VariationDimension)) { foreach($result->Variations->VariationDimensions->VariationDimension as $tmp) { $dimmension[$tmp] = apply_filters( 'atkp_variation_name', $tmp); } } else { $tmp = $result->Variations->VariationDimensions->VariationDimension; $dimmension[$tmp] = apply_filters( 'atkp_variation_name', $tmp); } foreach ( $vars as $varationItem ) { $myprd = $this->fill_product($varationItem, $result); $dimmfullname = array(); foreach($dimmension as $dimmy => $caption) { if(isset($varationItem->ItemAttributes->$dimmy)) $dimmfullname[$dimmy] = $varationItem->ItemAttributes->$dimmy; else $dimmfullname[$dimmy] = ''; } $myprd->variationname = $dimmfullname; array_push($variations, $myprd); } } $myproduct->variationname = $dimmension; $myproduct->variations = $variations; if ( isset( $result->DetailPageURL ) ) { $myproduct->producturl = urldecode( $result->DetailPageURL ); //für 100% im titel - replace muss geklärt werden.. $myproduct->producturl = str_replace( '%', '%25', $myproduct->producturl ); $myproduct->addtocarturl = $this->checkurl( 'http://www.amazon.' . $this->country . '/gp/aws/cart/add.html?AWSAccessKeyId=' . $this->accessKey . '&AssociateTag=' . $this->associateTag . '&ASIN.1=' . $myproduct->asin . '&Quantity.1=1' ); } else if ( $parentResult != null && isset( $parentResult->DetailPageURL ) ) { $myproduct->producturl = urldecode( $parentResult->DetailPageURL ); //für 100% im titel - replace muss geklärt werden.. $myproduct->producturl = str_replace( '%', '%25', $myproduct->producturl ); $myproduct->producturl = $this->checkurl( 'https://www.amazon.' . $this->country . '/dp/'.$myproduct->asin.'?tag='.$this->associateTag); $myproduct->addtocarturl = $this->checkurl( 'https://www.amazon.' . $this->country . '/gp/aws/cart/add.html?AWSAccessKeyId=' . $this->accessKey . '&AssociateTag=' . $this->associateTag . '&ASIN.1=' . $parentResult->ASIN . '&Quantity.1=1' ); } else { $myproduct->producturl = ''; $myproduct->addtocarturl = ''; } //TODO: ssl untersützung der bilder implementieren: https://images-na.ssl-images-amazon.com statt http://ecx.images-amazon.com if ( isset( $result->SmallImage->URL ) ) { $myproduct->smallimageurl = $this->checkimageurl( $result->SmallImage->URL, 'small' ); } if ( isset( $result->MediumImage->URL ) ) { $myproduct->mediumimageurl = $this->checkimageurl( $result->MediumImage->URL, 'medium' ); } if ( isset( $result->LargeImage->URL ) ) { $myproduct->largeimageurl = $this->checkimageurl( $result->LargeImage->URL, 'large' ); } if ( $this->load_customer_reviews ) { if ( $result->CustomerReviews->HasReviews ) { $averageRating = 0; $totalReviews = 0; $this->get_customer_rating( $myproduct->asin, $averageRating, $totalReviews ); $myproduct->rating = $averageRating; $myproduct->reviewcount = $totalReviews; } else { $myproduct->rating = 0; $myproduct->reviewcount = 0; } } $myproduct->customerreviewurl = $this->checkurl( 'http://www.amazon.' . $this->country . '/product-reviews/' . $myproduct->asin . '/?tag=' . $this->associateTag ); //$myproduct->reviewsurl = $result->CustomerReviews->IFrameURL; $description = ''; if ( isset( $result->EditorialReviews->EditorialReview ) ) { if ( isset( $result->EditorialReviews->EditorialReview->Content ) ) { $description = $result->EditorialReviews->EditorialReview->Content; } elseif ( sizeof( $result->EditorialReviews->EditorialReview ) > 0 ) { foreach ( $result->EditorialReviews->EditorialReview as $tmp ) { if ( isset( $description ) && strlen( $description ) < strlen( $tmp->Content ) ) { $description = $tmp->Content; } } } } $description = ( isset( $description ) ) ? $description : ''; $features = ''; if ( isset( $result->ItemAttributes->Feature ) ) { if ( is_array( $result->ItemAttributes->Feature ) && sizeof( $result->ItemAttributes->Feature) > 0 ) { foreach ( $result->ItemAttributes->Feature as $feature ) { $features .= '
  • ' . $feature . '
  • '; } } } $myproduct->features = $features == '' ? '' : ''; $myproduct->description = $description; $myproduct->title = isset( $result->ItemAttributes ) ? htmlentities( $result->ItemAttributes->Title ) : ''; //preise laden if ( isset( $result->ItemAttributes->ListPrice->FormattedPrice ) ) { $myproduct->listprice = $result->ItemAttributes->ListPrice->FormattedPrice; } if ( $myproduct->listprice == '' && isset( $result->Offers->Offer->OfferListing->Price->FormattedPrice ) ) { $myproduct->listprice = $result->Offers->Offer->OfferListing->Price->FormattedPrice; } if ( isset( $result->Offers->Offer->OfferListing->Price->FormattedPrice ) ) { $myproduct->saleprice = $result->Offers->Offer->OfferListing->Price->FormattedPrice; } $infosset = false; if ( isset( $result->Offers->Offer->OfferListing->SalePrice->FormattedPrice ) ) { $myproduct->saleprice = $result->Offers->Offer->OfferListing->SalePrice->FormattedPrice; } //http://docs.aws.amazon.com/AWSECommerceService/latest/DG/RG_VariationOffers.html#RG_VariationOffers-rest if ( isset( $result->Variations->Item->Offers ) ) { foreach ( $result->Variations->Item->Offers as $offerVariation ) { if ( isset( $offerVariation->OfferListing->Price->FormattedPrice ) ) { $myproduct->listprice = ''; $myproduct->saleprice = $offerVariation->OfferListing->Price->FormattedPrice; if ( isset( $offerVariation->OfferListing->IsEligibleForPrime ) ) { $myproduct->isprime = $offerVariation->OfferListing->IsEligibleForPrime; } if ( isset( $offerVariation->OfferListing->Availability ) ) { $myproduct->availability = $offerVariation->OfferListing->Availability; } if ( isset( $offerVariation->OfferListing->PercentageSaved ) ) { $myproduct->percentagesaved = $offerVariation->OfferListing->PercentageSaved; } if ( isset( $offerVariation->OfferListing->AmountSaved->FormattedPrice ) ) { $myproduct->amountsaved = $offerVariation->OfferListing->AmountSaved->FormattedPrice; } $infosset = true; break; } } } if ( ! $infosset ) { if ( isset( $result->Offers->Offer->OfferListing->AmountSaved->FormattedPrice ) ) { $myproduct->amountsaved = $result->Offers->Offer->OfferListing->AmountSaved->FormattedPrice; } if ( isset( $result->Offers->Offer->OfferListing->PercentageSaved ) ) { $myproduct->percentagesaved = $result->Offers->Offer->OfferListing->PercentageSaved; } if ( isset( $result->Offers->Offer->OfferListing->Availability ) ) { $myproduct->availability = $result->Offers->Offer->OfferListing->Availability; } if ( isset( $result->Offers->Offer->OfferListing->IsEligibleForPrime ) ) { $myproduct->isprime = $result->Offers->Offer->OfferListing->IsEligibleForPrime; } } if ( isset( $result->Offers->Offer->OfferListing->SalePrice->FormattedPrice ) && $myproduct->saleprice == '' ) { $myproduct->saleprice = $result->Offers->Offer->OfferListing->SalePrice->FormattedPrice; $myproduct->listprice = ''; $myproduct->percentagesaved = ''; } if ( isset( $result->Offers->Offer->OfferListing->Price->FormattedPrice ) && $myproduct->saleprice == '' ) { $myproduct->saleprice = $result->Offers->Offer->OfferListing->Price->FormattedPrice; $myproduct->listprice = ''; $myproduct->percentagesaved = ''; } //TODO: lowestnew liefert völlig sinnlose preise welche keinerlei gütligtkeit haben if ( isset( $result->OfferSummary->LowestNewPrice ) && $myproduct->saleprice == '' ) { $myproduct->saleprice = $result->OfferSummary->LowestNewPrice->FormattedPrice; $myproduct->listprice = ''; $myproduct->percentagesaved = ''; } if ( isset( $result->OfferSummary->LowestNewPrice->FormattedPrice ) ) { $myproduct->lowestnewprice = $result->OfferSummary->LowestNewPrice->FormattedPrice; } if ( isset( $result->OfferSummary->TotalNew ) ) { $myproduct->totalnew = $result->OfferSummary->TotalNew; } if ( $myproduct->listprice == '' ) { $myproduct->amountsaved = ''; } $myproduct->salepricefloat = $this->price_to_float( $myproduct->saleprice ); $myproduct->amountsavedfloat = $this->price_to_float( $myproduct->amountsaved ); $myproduct->listpricefloat = $this->price_to_float( $myproduct->listprice ); $myproduct->shippingfloat = (float) 0; if ( isset( $result->ItemAttributes->Manufacturer ) ) { $myproduct->manufacturer = $result->ItemAttributes->Manufacturer; } if ( isset( $result->ItemAttributes->Brand ) ) { $myproduct->brand = $result->ItemAttributes->Brand; } if ( isset( $result->ItemAttributes->ISBN ) ) { $myproduct->isbn = $result->ItemAttributes->ISBN; } if ( isset( $result->ItemAttributes->ProductGroup ) ) { $myproduct->productgroup = $result->ItemAttributes->ProductGroup; } if ( isset( $result->ItemAttributes->ReleaseDate ) ) { $myproduct->releasedate = $result->ItemAttributes->ReleaseDate; } if ( isset( $result->ItemAttributes->Author ) ) { if ( is_array( $result->ItemAttributes->Author ) ) { $myproduct->author = implode( ', ', $result->ItemAttributes->Author ); } else { $myproduct->author = $result->ItemAttributes->Author; } } if ( isset( $result->ItemAttributes->NumberOfPages ) ) { $myproduct->numberofpages = $result->ItemAttributes->NumberOfPages; } $myproduct->mpn = ''; if ( isset( $result->ItemAttributes->ItemPartNumber ) ) { $myproduct->mpn = $result->ItemAttributes->ItemPartNumber; } $myproduct->ean = ''; if ( isset( $result->ItemAttributes->EAN ) ) { $myproduct->ean = $result->ItemAttributes->EAN; } if ( isset( $result->ItemAttributes->EANList ) && isset( $result->ItemAttributes->EANList->EANListElement ) ) { $newean = ''; if ( is_array( $result->ItemAttributes->EANList->EANListElement ) || is_object( $result->ItemAttributes->EANList->EANListElement ) ) { foreach ( $result->ItemAttributes->EANList->EANListElement as $eanitem ) { if ( $newean == '' ) { $newean = $eanitem; } else { $newean .= ',' . $eanitem; } } } if ( $newean != '' ) { $myproduct->ean = $newean; } } $myproduct->imagesurl = ''; $myproduct->thumbimagesurl = ''; $images = array(); //if(is_array($result->ImageSets->ImageSet)) //echo serialize($result->ImageSets->ImageSet); if ( isset( $result->ImageSets->ImageSet ) ) { foreach ( $result->ImageSets->ImageSet as $i => $image ) { if ( ! isset( $image->SmallImage->URL ) && ! isset( $image->MediumImage->URL ) && ! isset( $image->LargeImage->URL ) ) { continue; } $udf = new atkp_product_image(); $udf->id = uniqid(); if ( isset( $image->SmallImage->URL ) ) { $udf->smallimageurl = $this->checkimageurl( $image->SmallImage->URL, 'small' ); } if ( isset( $image->MediumImage->URL ) ) { $udf->mediumimageurl = $this->checkimageurl( $image->MediumImage->URL, 'medium' ); } if ( isset( $image->LargeImage->URL ) ) { $udf->largeimageurl = $this->checkimageurl( $image->LargeImage->URL, 'large' ); } array_push( $images, $udf ); } } $myproduct->images = $images; if(count($myproduct->variations) > 0 && $myproduct->salepricefloat == 0) { foreach($myproduct->variations as $variation) { if($variation->salepricefloat > 0) { $myproduct->listprice = $variation->listprice; $myproduct->amountsaved = $variation->amountsaved; $myproduct->saleprice = $variation->saleprice; $myproduct->listpricefloat = $variation->listpricefloat; $myproduct->amountsavedfloat = $variation->amountsavedfloat; $myproduct->percentagesaved = $variation->percentagesaved; $myproduct->salepricefloat = $variation->salepricefloat; $myproduct->shippingfloat = $variation->shippingfloat; $myproduct->availability = $variation->availability; $myproduct->shipping = $variation->shipping; $myproduct->isprime = $variation->isprime; $myproduct->smallimageurl = $variation->smallimageurl; $myproduct->mediumimageurl = $variation->mediumimageurl; $myproduct->largeimageurl = $variation->largeimageurl; break; } } } //echo serialize($result);exit; //echo serialize($myproduct);exit; return $myproduct; } private function get_customer_rating( $asin, &$averageRating, &$totalReviews ) { try { $url = 'http://www.amazon.' . $this->country . '/product-reviews/' . $asin; $rating = false; if ( function_exists( 'wp_remote_get' ) ) { $response = wp_remote_get( $url, array( 'timeout' => 20, 'user-agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' ) ); $statusCode = null; if ( function_exists( 'is_wp_error' ) && ! is_wp_error( $response ) ) { // Success if ( isset( $response['response']['code'] ) ) { $statusCode = $response['response']['code']; } if ( '200' == $statusCode ) { $page = $response['body']; } } } if ( ! empty ( $page ) ) { $rating = $this->scrape_customer_rating( $page, $averageRating, $totalReviews ); } if ( $rating === false ) { //echo '2nd try!
    '; if ( ini_get( 'allow_url_fopen' ) ) { try { // Trying to use file_get_contents $opts = array( 'http' => array( 'header' => 'Connection: close', 'ignore_errors' => true ) ); $context = stream_context_create( $opts ); @$page = file_get_contents( $url, false, $context ); $rating = $this->scrape_customer_rating( $page, $averageRating, $totalReviews ); } catch ( Exception $ex ) { // Do nothing } } } } catch ( Exception $e ) { //echo 'Error: ' .$e->getMessage(); } } public function scrape_customer_rating( $page, &$averageRating, &$totalReviews ) { if ( ! class_exists( 'simple_html_dom' ) ) { require_once ATKP_PLUGIN_DIR . '/lib/simple_html_dom.php'; } $html = new simple_html_dom(); $html->load( $page ); if ( ! $html ) { return false; } // Reviews $label_reviews = $html->find( 'span.totalReviewCount', 0 ); $label_reviews = ( isset( $label_reviews->plaintext ) ) ? $label_reviews->plaintext : 0; $label_reviews = preg_replace( "/[^0-9]/", "", $label_reviews ); if ( '0' == $label_reviews ) { $averageRating = 0; $totalReviews = 0; return true; } if ( is_numeric( $label_reviews ) ) { $totalReviews = intval( $label_reviews ); } // Rating $label_rating = $html->find( 'div.averageStarRatingNumerical', 0 ); $label_rating = ( isset( $label_rating->plaintext ) ) ? $label_rating->plaintext : 0; if ( ! empty( $label_rating ) ) { $label_rating_parts = explode( ' ', $label_rating ); if ( ! empty( $label_rating_parts[0] ) ) { $label_rating = str_replace( ',', '.', $label_rating_parts[0] ); $averageRating = $label_rating; return true; } } // Return return false; } public function get_supportedlistsources() { return '10,11,20,30,40'; } public function retrieve_list( $requestType, $rootCategory, $keyword, $asin, $maxCount, $sortorder, $filters ) { if ( $this->helper == null ) { throw new Exception( 'checklogon required' ); } $mylist = new atkp_list_resp(); $mylist->updatedon = current_time( 'timestamp' ); $mylist->asins = array(); $mylist->products = null; switch ( $requestType ) { case 'Similarity': $search = new ApaiIO\Operations\SimilarityLookup(); $search->setPage( 1 ); if ( $rootCategory == '' ) { $search->setCategory( 'All' ); } else { $search->setCategory( $rootCategory ); } $search->setItemId( $asin ); try { $response = $this->helper->runOperation( $search ); $response = $this->convert_response( $response ); $check = $this->checkResponse( $response ); } catch ( GuzzleHttp\Exception\ClientException $e ) { $response = $e->getResponse(); if ( isset( $response ) ) { $check = $response->getBody(); } else { $check = $e->getMessage(); } } catch ( Exception $e ) { $check = $e->getMessage(); } //falls fehler aufgetreten ist, 2. versuch if ( ! empty( $check ) ) { sleep( 3 ); $response = $this->helper->runOperation( $search ); $response = $this->convert_response( $response ); $check = $this->checkResponse( $response ); } if ( ! empty( $check ) ) { throw new Exception( $check ); } if ( isset( $response->Items->Item ) ) { if ( isset( $response->Items->MoreSearchResultsUrl ) ) { $mylist->listurl = $response->Items->MoreSearchResultsUrl; } //loop through each item foreach ( $response->Items->Item as $result ) { //check that there is a ASIN code - for some reason, some items are not //correctly listed. Im sure there is a reason for it, need to check. if ( isset( $result->ASIN ) ) { array_push( $mylist->asins, $result->ASIN ); } } } break; case 'TopSellers': $browseNodeLookup = new ApaiIO\Operations\BrowseNodeLookup(); $browseNodeLookup->setNodeId( intval( $rootCategory ) ); $browseNodeLookup->setResponseGroup( array( 'TopSellers' ) ); try { $response = $this->helper->runOperation( $browseNodeLookup ); $response = $this->convert_response( $response ); $check = $this->checkResponse( $response ); } catch ( GuzzleHttp\Exception\ClientException $e ) { $response = $e->getResponse(); if ( isset( $response ) ) { $check = $response->getBody(); } else { $check = $e->getMessage(); } } catch ( Exception $e ) { $check = $e->getMessage(); } //falls fehler aufgetreten ist, 2. versuch if ( ! empty( $check ) ) { sleep( 3 ); $response = $this->helper->runOperation( $browseNodeLookup ); $response = $this->convert_response( $response ); $check = $this->checkResponse( $response ); } if ( ! empty( $check ) ) { throw new Exception( $check ); } if ( isset( $response->BrowseNodes ) && isset( $response->BrowseNodes->BrowseNode ) ) { $mylist->browsenodename = $response->BrowseNodes->BrowseNode->Name; if ( isset( $response->BrowseNodes->BrowseNode->TopSellers ) ) { $response = $response->BrowseNodes->BrowseNode->TopSellers->TopSeller; if ( is_array( $response ) ) { foreach ( $response as $item ) { array_push( $mylist->asins, $item->ASIN ); } } } } break; case 'NewReleases': $browseNodeLookup = new ApaiIO\Operations\BrowseNodeLookup(); $browseNodeLookup->setNodeId( intval( $rootCategory ) ); $browseNodeLookup->setResponseGroup( array( 'NewReleases' ) ); try { $response = $this->helper->runOperation( $browseNodeLookup ); $response = $this->convert_response( $response ); $check = $this->checkResponse( $response ); } catch ( GuzzleHttp\Exception\ClientException $e ) { $response = $e->getResponse(); if ( isset( $response ) ) { $check = $response->getBody(); } else { $check = $e->getMessage(); } } catch ( Exception $e ) { $check = $e->getMessage(); } //falls fehler aufgetreten ist, 2. versuch if ( ! empty( $check ) ) { sleep( 3 ); $response = $this->helper->runOperation( $browseNodeLookup ); $response = $this->convert_response( $response ); $check = $this->checkResponse( $response ); } if ( ! empty( $check ) ) { throw new Exception( $check ); } if ( isset( $response->BrowseNodes ) && isset( $response->BrowseNodes->BrowseNode ) ) { $mylist->browsenodename = $response->BrowseNodes->BrowseNode->Name; if ( isset( $response->BrowseNodes->BrowseNode->NewReleases->NewRelease ) ) { $response = $response->BrowseNodes->BrowseNode->NewReleases->NewRelease; if ( is_array( $response ) ) { foreach ( $response as $item ) { array_push( $mylist->asins, $item->ASIN ); } } } } break; case 'ExtendedSearch': case 'Search': $search = new ApaiIO\Operations\Search(); if ( $rootCategory == '' ) { $search->setCategory( 'All' ); } else { $search->setCategory( $rootCategory ); } //Page: Valid values: 1 to 10 (1 to 5 when search index is All) if ( $rootCategory == '' || $rootCategory == 'All' ) { if($maxCount > 50) $maxCount = 50; } $search->setResponseGroup( array( 'Large', 'Variations', 'OfferFull', 'Variations' ) ); if ( $rootCategory != '' && $rootCategory != 'All' && $sortorder != '' ) { $search->setSort( $sortorder ); } if ( $filters != null ) { foreach ( $filters as $field => $value ) { if ( $field == 'Keywords' ) { $keyword = $value; } else if ( $field == 'SearchIndex' ) { $search->setCategory( $value ); } else { $search->setOperationParameter( $field, $value ); } } } $search->setKeywords( $keyword ); $products = array(); $pagination = 0; do { $pagination ++; //increment $search->setPage( $pagination ); try { $response = $this->helper->runOperation( $search ); $response = $this->convert_response( $response ); $check = $this->checkResponse( $response ); } catch ( GuzzleHttp\Exception\ClientException $e ) { $response = $e->getResponse(); if ( isset( $response ) ) { $check = $response->getBody(); } else { $check = $e->getMessage(); } } catch ( Exception $e ) { $check = $e->getMessage(); } //falls fehler aufgetreten ist, 2. versuch if ( ! empty( $check ) ) { sleep( 3 ); $response = $this->helper->runOperation( $search ); $response = $this->convert_response( $response ); $check = $this->checkResponse( $response ); } if ( ! empty( $check ) ) { throw new Exception( $check ); } //check that there are items in the response if ( isset( $response->Items->Item ) ) { $mylist->listurl = $response->Items->MoreSearchResultsUrl; $items = array(); if(is_array( $response->Items->Item)) $items = $response->Items->Item; else array_push($items, $response->Items->Item); //loop through each item foreach ( $items as $result ) { //check that there is a ASIN code - for some reason, some items are not //correctly listed. Im sure there is a reason for it, need to check. if ( isset( $result->ASIN ) ) { array_push( $mylist->asins, $result->ASIN ); $myproduct = $this->fill_product( $result ); array_push( $products, $myproduct ); } if ( count( $products ) >= $maxCount ) { break; } } //wenn weniger als 10 ergebnisse kommen, abbrechen if ( count( $response->Items->Item ) < 10 ) { break; } if ( count( $products ) >= $maxCount ) { break; } } } while ( ( $pagination * 10 ) <= $maxCount ); $mylist->products = $products; break; default: throw new Exception( 'unknown requestType: ' . $requestType ); break; } if ( $mylist->products == null && count( $mylist->asins ) > 0 ) { //wenn produkte nicht direkt geladen, dann nachladen $response = $this->retrieve_products( $mylist->asins, 'ASIN' ); $products = array(); foreach ( $response->responseitems as $responseitem ) { array_push( $products, $responseitem->productitem ); } $mylist->products = $products; } return $mylist; } public function check_offer( $offer ) { $products = null; $found = false; if ( $offer->type == 1 ) { $products = $this->retrieve_products_internal( $offer->number, 'EAN' ); } else { $products = $this->retrieve_products_internal( $offer->number, 'ASIN' ); } $offer->pricefloat = 0; if ( $products != null && count( $products ) > 0 ) { foreach ( $products as $product ) { if ( $offer->pricefloat == 0 || ( $offer->pricefloat > $product->salepricefloat && $product->salepricefloat <> 0 ) ) { $offer->shipping = ''; $offer->availability = $product->availability; $offer->price = $product->saleprice; $offer->pricefloat = $product->salepricefloat; $offer->shippingfloat = (float) 0; $offer->link = $product->producturl; $offer->title = htmlspecialchars( $product->title ); $found = true; } } } if ( ! $found ) { $offer->message = 'product not found'; } return $offer; } } ?>