Amazon Advertising API get Node's products - php

I'm creating e-commerce shop with Amazon API integration.
The problem I faced with is I cannot get items from specific node.
So I've tried many ways to do that, last one was something like this:
$fields = array();
$fields['AssociateTag'] = "ItemSearch";
$fields['Condition'] = 'All';
$fields['Operation'] = 'ItemSearch';
$fields['Version'] = '2013-08-01';
$fields['BrowseNode'] = $catId;
$fields['ResponseGroup'] = "Images,ItemAttributes,Offers";
$fields['Service'] = 'AWSECommerceService';
$fields['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
$fields['AWSAccessKeyId'] = $this->accessKey;
After that call I have the output:
public 'Items' =>
object(SimpleXMLElement)[150]
public 'Request' =>
object(SimpleXMLElement)[139]
public 'IsValid' => string 'True' (length=4)
public 'ItemSearchRequest' =>
object(SimpleXMLElement)[138]
public 'BrowseNode' => string '1289481011' (length=10)
public 'Condition' => string 'All' (length=3)
public 'ResponseGroup' =>
array (size=3)
0 => string 'Images' (length=6)
1 => string 'ItemAttributes' (length=14)
2 => string 'Offers' (length=6)
So I can see my request, but no items were returned to me.
By the way, ItemLookup, ItemSearch with keywords and BrowseNode operations work just fine.
What can I do to get items from node with spicific ID without using keywords?

I figured out what happened. I didn't set SearchIndex when I point BrowseNode ID.
So there was no result.

Related

Zend route with unlimited parameters

I am using Zend 1.10. I am trying to create a route for unlimited variables like abc.com/var1/var2/var3 and so on.
Till now i have searched and find out that i can add route for each variable like
$route = new Zend_Controller_Router_Route(
'/emaillog/:var1',
array(
'controller' => 'emaillog', // The controller to point to.
'action' => 'index', // The action to point to, in said Controller.
':var1' =>null //default value if param is not passed
)
);
$frontController->getRouter()->addRoute('emaillogWithVar1', $route);
and for second variable,
$route = new Zend_Controller_Router_Route(
'/emaillog/:var1/:var2',
array(
'controller' => 'emaillog', // The controller to point to.
'action' => 'index', // The action to point to, in said Controller.
':var1' =>null, //default value if param is not passed
':var2' =>null //default value if param is not passed
)
);
$frontController->getRouter()->addRoute('emaillogWithVar2', $route);
But the work i going to do, can contain 1-∞ infinite variables.
So i want to have once route for unlimited variables.
Any help will be appreciated!
There is a way to get infinite params. For that you don't have to declare them.
Lest say the url is index/test/p1/p2/p3/p4/p5, now if you try the following inside IndexController:
public function testAction()
{
var_dump($this->getRequest());
}
You will get a dump of an instance of Zend_Controller_Request_Http, in that dump, you will see, 2 properties, _params & _pathInfo, Like:
protected '_pathInfo' => string '/index/test/p1/p2/p3/p4/p5' (length=26)
protected '_params' =>
array (size=5)
'controller' => string 'index' (length=5)
'action' => string 'test' (length=4)
'p1' => string 'p2' (length=2)
'p3' => string 'p4' (length=2)
'module' => string 'default' (length=7)
1st method: Now in _params, they will occur as key => value pairs, as long as they are even, the last odd param will be ignored. You can get these by(for example):
$params = $this->getRequest()->getParams();
unset($params['controller']);
unset($params['action']);
unset($params['module']);
$params = array_merge(array_keys($params), array_values($params));
var_dump($params);
Output:
array (size=4)
0 => string 'p1' (length=2)
1 => string 'p3' (length=2)
2 => string 'p2' (length=2)
3 => string 'p4' (length=2)
2nd Method: OR you can use _pathinfo to get the params using explode, like:
$path = $this->getRequest()->getPathinfo();
$params = explode('/', $path);
unset($params[0]);
unset($params[1]);
unset($params[2]);
var_dump($params);
Output:
array (size=5)
3 => string 'p1' (length=2)
4 => string 'p2' (length=2)
5 => string 'p3' (length=2)
6 => string 'p4' (length=2)
7 => string 'p5' (length=2)

How to extends properly Cms Page and Block WebAPI

I would like to extends/override the APIs of CMS Pages and Blocks to add custom fields (for example, an array of store_ids to allow the creation of a CMS block on different stores with an webapi call, it's my main goal).
I tried many things, but this does not seem clean, standards-compliant. I have the impression of overloading the whole module CMS (data interface, repository interface, model, repository factory, etc..) just for adding new fields in webapi.
I will explain what I have done for now :
1/ I wrote an new data api \BlockInterface who extends the core interface to add my new field (store_id) and declare his getter and setter methods.
namespace Reflet\CmsExtension\Api\Data;
interface BlockInterface extends \Magento\Cms\Api\Data\BlockInterface
{
/**
* New fields
*/
const STORE_ID = 'store_id';
/**
* Get Store Ids
*
* #return array
*/
public function getStoreId();
/**
* Set Store Ids
*
* #param array $storeIds
* #return \Reflet\CmsExtension\Api\Data\BlockInterface
*/
public function setStoreId($storeIds);
}
2/ I implements the Data Interface in the new \Block model object.
3/ With many in the di.xml file, I override the block model type.
4/ I reimplement the api \BlockRepositoryInterface and the webapi.xml file to change the data type used in the repository.
5/ I testing the getById() and the save() methods for the block #1 with simple repository call and webapi.
Here, is my result :
1/ If, I get the block #1 with the repository, the store_id is load in the result object.
array (size=12)
'row_id' => string '1' (length=1)
'block_id' => string '1' (length=1)
'created_in' => string '1' (length=1)
'updated_in' => string '2147483647' (length=10)
'title' => string 'Catalog Events Lister' (length=21)
'identifier' => string 'catalog_events_lister' (length=21)
'content' => string '{{block class="Magento\\CatalogEvent\\Block\\Event\\Lister" name="catalog.event.lister" template="lister.phtml"}}' (length=113)
'creation_time' => string '2017-02-14 14:33:20' (length=19)
'update_time' => string '2017-02-14 14:33:20' (length=19)
'is_active' => string '1' (length=1)
'store_id' =>
array (size=1)
0 => string '0' (length=1)
'stores' =>
array (size=1)
0 => string '0' (length=1)
2/ If, I get the block #1 with the WebAPI, the store_id is also load in the result object.
public 'store_id' =>
array (size=1)
0 => string '0' (length=1)
public 'id' => int 1
public 'identifier' => string 'catalog_events_lister' (length=21)
public 'title' => string 'Catalog Events Lister' (length=21)
public 'content' => string '{{block class="Magento\\CatalogEvent\\Block\\Event\\Lister" name="catalog.event.lister" template="lister.phtml"}}' (length=113)
public 'creation_time' => string '2017-02-14 14:33:20' (length=19)
public 'update_time' => string '2017-02-14 14:33:20' (length=19)
public 'active' => boolean true
3/ If, I save the block #1 with the WebAPI, an error occurs, he load the wrong BlockInterface. I need to reimplement the BlockRepository ? I don't think it's the good and cleaner method (...) ?
public 'message' => string 'Property "StoreId" does not have corresponding setter in class "Magento\Cms\Api\Data\BlockInterface".' (length=101)
public 'trace' => string '#0 /var/www/vhosts/reflet.com/storemanager/vendor/magento/framework/Reflection/NameFinder.php(59): Magento\Framework\Reflection\NameFinder->findAccessorMethodName(Object(Zend\Code\Reflection\ClassReflection), 'StoreId', 'getStoreId', 'isStoreId')
#1 /var/www/vhosts/reflet.com/storemanager/vendor/magento/framework/Webapi/ServiceInputProcessor.php(158): Magento\Framework\Reflection\NameFinder->getGetterMethodName(Object(Zend\Code\Reflection\ClassReflection), 'StoreId')
#2 /var/www/vhosts/reflet.com/storemanag'... (length=2239)
For this example, I use the 'store_id' only, but in the future, it's must be works with all custom fields in Page or Block object.
How do you recommend this modification to be as clean as possible and limit conflicts in the future ? Do you have an example ?
If you want to test, you can find in this archive, my two current Magento modules (Reflet_Api an simple adapter to call REST WebApi and Reflet_CmsExtension to override CMS Blocks) : https://drive.google.com/file/d/0B12trf96j0ALSjhZdmJPTzBPT0U/view
Thank you for your answers.
Best regards.
Jonathan

Phalcon: json_encode(array()) return nothing

So here's my issue:
I am fetching data in my database and want to provide them in jSON format.
My controller is the following:
public function testAction()
{
$articles = Article::find();
if (count($articles) > 0) {
$final_array = array();
foreach ($articles as $article) {
$user = Users::find("id = " . $article->getUsersId());
$current = array('id' => $article->getId(),
'name' => $article->getName(),
'replies' => $article->getReplies(),
'date' => $article->getDate(),
'illustration' => $article->getIllustration(),
'content' => $article->getContent(),
'link' => $article->getLink(),
'user_id' => $article->getUsersId(),
'user_name' => $user[0]->getPseudo());
$final_array[] = $current;
}
$result = array('status' => 1,
'message' => 'article have been downloaded',
'response' => $final_array);
} else {
$result = array('status' => 1,
'message' => 'no article in the stack');
}
$this->view->disable();
$this->response->setContentType('application/json', 'UTF-8');
echo json_encode($result);
}
The view displayed provide nothing:
<html>
<head></head>
<body></body>
</html>
The trouble doesn't come from my model or SQL request, because if I var_dump my result instead by changing my controller like this:
[...]
//$this->view->disable();
//$this->response->setContentType('application/json', 'UTF-8');
var_dump($result);
[...]
It provides me the following (length doesn't match all the time because I voluntary changed the content which is not interesting in this case):
array (size=3)
'status' => int 1
'message' => string 'article have been downloaded' (length=28)
'response' =>
array (size=1)
0 =>
array (size=9)
'id' => string '1' (length=1)
'name' => string 'Champion de CAPU' (length=16)
'replies' => string '0' (length=1)
'date' => string '2014-06-10 06:22:35' (length=19)
'illustration' => string 'illustration_link' (length=69)
'content' => string 'content_text' (length=182)
'link' => string 'more_link' (length=50)
'user_id' => string '6' (length=1)
'user_name' => string 'bathiatus' (length=9)
which is what I want to get...
Moreover, I actually did the same in another controller in order to provide all the users (so UserController, jGetAllUsersAction) and it works pretty well (the code is the same except that the table in the database are different).
I finally figured out the issue.
Thank you for the answers, I found that all purposed way to display the view is working (including the one I purposed in my question).
I don't really know which way is the best, but I guess the one in my question is not.
By the way, the issue was that I was trying to inject special characters (such as é, è, à, ë, ...) in my Json object. Indeed, my content is in french.
Json object do not support these kind of characters while printing them with a var_dump presents no issue.
If you want just a json response you can do this in your controller:
return $this->response->setJsonContent($result);
Phalcon disables the view and sets the right content type automatically with that. The json_encode is also done, so just put in your $result.
By default Phalcon needs a view to return the response (in your case the JSON).
Controller code:
$this->response->setContentType('application/json', 'UTF-8');
$this->view->setVar("some_var", $result);
Then you have to create a view corresponing to the name of the controller and the function
and put there:
<?php echo json_encode($some_var); ?>
If you don't want to crate a additional view, please use this link for further reference:
http://docs.phalconphp.com/en/latest/reference/response.html

MailChimp API listStaticSegments is always empty

I have created several static Segments in Mailchimp for a List.
I am writing some PHP to create campaigns, but I am stuck on a hurdle early on.
I am using v1.3 of the MailChimp API
$api = new MCAPI( $this->config['api_key'] );
$lists = $api->listStaticSegments( $list_id );
var_dump( $api );
var_dump( $lists );
Results:
object(MCAPI)[39]
public 'version' => string '1.3' (length=3)
public 'errorMessage' => string '' (length=0)
public 'errorCode' => string '' (length=0)
public 'apiUrl' =>
array (size=4)
'scheme' => string 'http' (length=4)
'host' => string 'api.mailchimp.com' (length=17)
'path' => string '/1.3/' (length=5)
'query' => string 'output=php' (length=10)
public 'timeout' => int 300
public 'chunkSize' => int 8192
public 'api_key' => string 'XXX' (length=XX)
public 'secure' => boolean false
array (size=0)
empty
I am expecting $lists to have 10 items in an array.
When I log into MailChimp and check the API Account API Keys page, I can see there are requests but there are no responses.
Ah ha. The problem was I had no static lists. The lists that I had created were dynamic segments (auto updating) , so therefore I must use campaignSegmentTest to generate the same criteria for the dynamic segments.
http://apidocs.mailchimp.com/api/1.3/campaignsegmenttest.func.php
Further research on the Google Mailchimp forum:
https://groups.google.com/forum/#!msg/mailchimp-api-discuss/63DLfYq7ydI/jyGASmUxK8AJ

Amazon PHP SDK Listing Objects

I have the need to display the bucket contents on my S3 and I am using Amazon's PHP SDK.
My code is simply
$objects = $s3->list_objects("mybucket",array("max-keys"=>5));
var_dump($objects);
The response I get from the server is very complicated for me to understand -
It's essence is
Object(CFResponse)[107]
public 'header' =>
array (size=11)
'x-amz-id-2' => string
...
public 'body' =>
object(CFSimpleXML)[106]
public '#attributes' =>
array (size=1)
'ns' => string 'http://s3.amazonaws.com/doc/2006-03-01/' (length=39)
public 'Name' => string 'cdneu.2yourfacecdn.com' (length=22)
public 'Prefix' =>
object(CFSimpleXML)[3]
public 'Marker' =>
object(CFSimpleXML)[105]
public 'MaxKeys' => string '5' (length=1)
public 'IsTruncated' => string 'true' (length=4)
public 'Contents' =>
array (size=5)
0 =>
object(CFSimpleXML)[104]
...
1 =>
object(CFSimpleXML)[103]
...
2 =>
object(CFSimpleXML)[102]
...
3 =>
object(CFSimpleXML)[101]
...
4 =>
object(CFSimpleXML)[100]
...
public 'status' => int 200
I believe the part under the 'Contents' is what I'm looking for but how do I access it ? I'm used to receiving arrays where I can figure out what the keys are and how to access but this here is difficult for me ,
Any guesses?
try this in order to list the key element of each object:
$s3 = new AmazonS3();
$objects = $s3->list_objects("YOUR BUCKET NAME",array("max-keys"=>5));
foreach ($objects->body->Contents as $item){
print_r($item->Key."");
}
You can access contents as follows
$contents = $objects['Contents'];

Categories