Magento 2. Make soap response as array
I'm implementing custom API for Magento 2. My client want the response to be as associative array.
Here is my class method:
class Gcapiapi implements GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return array
*/
public function list($products = NULL)
{
$result = array();
$stockItems = ...
...
foreach($stockItems as $stockItem){
$itemData = array('product_id' => $product->getId(), 'sku' => $productSku, 'qty' => $stockItem->getQty(), 'is_in_stock' => $stockItem->getIsInStock());
$result = $itemData;
}
return $result;
}
}
Here is interface declaration:
interface GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return array
*/
public function list($products = NULL);
}
I've added logs and I see that my method list executing. But in response I'm getting 500 error.
In exception.log I see the error:
Message: Class "array" does not exist. Please note that namespace must be specified.
I want to get the following response:
array (size=2)
0 =>
array (size=4)
'product_id' => string '3708' (length=4)
'sku' => string 'W3L2221LDCB2' (length=12)
'qty' => string '228.0000' (length=8)
'is_in_stock' => string '1' (length=1)
1 =>
array (size=4)
'product_id' => string '3709' (length=4)
'sku' => string 'W7L1226E5C96' (length=12)
'qty' => string '23.0000' (length=7)
'is_in_stock' => string '1' (length=1)
Can I get SOAP response as associative array somehow?
Thanks,
magento2 api soap
bumped to the homepage by Community♦ 8 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I'm implementing custom API for Magento 2. My client want the response to be as associative array.
Here is my class method:
class Gcapiapi implements GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return array
*/
public function list($products = NULL)
{
$result = array();
$stockItems = ...
...
foreach($stockItems as $stockItem){
$itemData = array('product_id' => $product->getId(), 'sku' => $productSku, 'qty' => $stockItem->getQty(), 'is_in_stock' => $stockItem->getIsInStock());
$result = $itemData;
}
return $result;
}
}
Here is interface declaration:
interface GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return array
*/
public function list($products = NULL);
}
I've added logs and I see that my method list executing. But in response I'm getting 500 error.
In exception.log I see the error:
Message: Class "array" does not exist. Please note that namespace must be specified.
I want to get the following response:
array (size=2)
0 =>
array (size=4)
'product_id' => string '3708' (length=4)
'sku' => string 'W3L2221LDCB2' (length=12)
'qty' => string '228.0000' (length=8)
'is_in_stock' => string '1' (length=1)
1 =>
array (size=4)
'product_id' => string '3709' (length=4)
'sku' => string 'W7L1226E5C96' (length=12)
'qty' => string '23.0000' (length=7)
'is_in_stock' => string '1' (length=1)
Can I get SOAP response as associative array somehow?
Thanks,
magento2 api soap
bumped to the homepage by Community♦ 8 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I'm implementing custom API for Magento 2. My client want the response to be as associative array.
Here is my class method:
class Gcapiapi implements GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return array
*/
public function list($products = NULL)
{
$result = array();
$stockItems = ...
...
foreach($stockItems as $stockItem){
$itemData = array('product_id' => $product->getId(), 'sku' => $productSku, 'qty' => $stockItem->getQty(), 'is_in_stock' => $stockItem->getIsInStock());
$result = $itemData;
}
return $result;
}
}
Here is interface declaration:
interface GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return array
*/
public function list($products = NULL);
}
I've added logs and I see that my method list executing. But in response I'm getting 500 error.
In exception.log I see the error:
Message: Class "array" does not exist. Please note that namespace must be specified.
I want to get the following response:
array (size=2)
0 =>
array (size=4)
'product_id' => string '3708' (length=4)
'sku' => string 'W3L2221LDCB2' (length=12)
'qty' => string '228.0000' (length=8)
'is_in_stock' => string '1' (length=1)
1 =>
array (size=4)
'product_id' => string '3709' (length=4)
'sku' => string 'W7L1226E5C96' (length=12)
'qty' => string '23.0000' (length=7)
'is_in_stock' => string '1' (length=1)
Can I get SOAP response as associative array somehow?
Thanks,
magento2 api soap
I'm implementing custom API for Magento 2. My client want the response to be as associative array.
Here is my class method:
class Gcapiapi implements GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return array
*/
public function list($products = NULL)
{
$result = array();
$stockItems = ...
...
foreach($stockItems as $stockItem){
$itemData = array('product_id' => $product->getId(), 'sku' => $productSku, 'qty' => $stockItem->getQty(), 'is_in_stock' => $stockItem->getIsInStock());
$result = $itemData;
}
return $result;
}
}
Here is interface declaration:
interface GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return array
*/
public function list($products = NULL);
}
I've added logs and I see that my method list executing. But in response I'm getting 500 error.
In exception.log I see the error:
Message: Class "array" does not exist. Please note that namespace must be specified.
I want to get the following response:
array (size=2)
0 =>
array (size=4)
'product_id' => string '3708' (length=4)
'sku' => string 'W3L2221LDCB2' (length=12)
'qty' => string '228.0000' (length=8)
'is_in_stock' => string '1' (length=1)
1 =>
array (size=4)
'product_id' => string '3709' (length=4)
'sku' => string 'W7L1226E5C96' (length=12)
'qty' => string '23.0000' (length=7)
'is_in_stock' => string '1' (length=1)
Can I get SOAP response as associative array somehow?
Thanks,
magento2 api soap
magento2 api soap
asked Nov 6 '17 at 13:51
HelmsmantestHelmsmantest
162
162
bumped to the homepage by Community♦ 8 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 8 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Rewrite your class as:
class Gcapiapi implements GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return mixed
*/
public function list($products = NULL)
{
$result = array();
$stockItems = ...
...
foreach($stockItems as $stockItem){
$itemData = array('product_id' => $product->getId(), 'sku' => $productSku, 'qty' => $stockItem->getQty(), 'is_in_stock' => $stockItem->getIsInStock());
$result = $itemData;
}
return $result;
}
}
You can see the change in return type: array
-> mixed
It's strongly advised to use Data interface in such cases even though mixed
will work for you.
In this case.Instead of associative array i'm getting std object:stdClass Object ( [result] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [key] => product_id [value] => 1 ) ...
– Helmsmantest
Nov 6 '17 at 15:12
then cast with(array) $result
after you get the result.
– MagePsycho
Nov 6 '17 at 15:13
Unfortunatelly we can not modify code on the client's side. Is there some other method to get associative array as the result of soap function calling?
– Helmsmantest
Nov 6 '17 at 16:01
Any updates, guys?
– Helmsmantest
Nov 9 '17 at 14:39
add a comment |
Once you've received the response from Magento, you can use the following snippet to convert the SOAP object into a PHP associative array:
<?php
$response = simplexml_load_string($soapResponse->any);
$response = json_decode(json_encode($response), true);
Assuming that $soapResponse->any
has this content:
<data count="1" count_available="1">
<row id="1">
<sku>1830DMG</sku>
<price>74.06</price>
<stockid>519745</stockid>
</row>
</data>
You'll get an array which looks like this:
$ php -f apitest3.php
array(2) {
["@attributes"]=>
array(2) {
["count"]=>
string(1) "1"
["count_available"]=>
string(1) "1"
}
["row"]=>
array(4) {
["@attributes"]=>
array(1) {
["id"]=>
string(1) "1"
}
["sku"]=>
string(7) "1830DMG"
["price"]=>
string(5) "74.06"
["stockid"]=>
string(6) "519745"
}
}
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f200224%2fmagento-2-make-soap-response-as-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Rewrite your class as:
class Gcapiapi implements GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return mixed
*/
public function list($products = NULL)
{
$result = array();
$stockItems = ...
...
foreach($stockItems as $stockItem){
$itemData = array('product_id' => $product->getId(), 'sku' => $productSku, 'qty' => $stockItem->getQty(), 'is_in_stock' => $stockItem->getIsInStock());
$result = $itemData;
}
return $result;
}
}
You can see the change in return type: array
-> mixed
It's strongly advised to use Data interface in such cases even though mixed
will work for you.
In this case.Instead of associative array i'm getting std object:stdClass Object ( [result] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [key] => product_id [value] => 1 ) ...
– Helmsmantest
Nov 6 '17 at 15:12
then cast with(array) $result
after you get the result.
– MagePsycho
Nov 6 '17 at 15:13
Unfortunatelly we can not modify code on the client's side. Is there some other method to get associative array as the result of soap function calling?
– Helmsmantest
Nov 6 '17 at 16:01
Any updates, guys?
– Helmsmantest
Nov 9 '17 at 14:39
add a comment |
Rewrite your class as:
class Gcapiapi implements GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return mixed
*/
public function list($products = NULL)
{
$result = array();
$stockItems = ...
...
foreach($stockItems as $stockItem){
$itemData = array('product_id' => $product->getId(), 'sku' => $productSku, 'qty' => $stockItem->getQty(), 'is_in_stock' => $stockItem->getIsInStock());
$result = $itemData;
}
return $result;
}
}
You can see the change in return type: array
-> mixed
It's strongly advised to use Data interface in such cases even though mixed
will work for you.
In this case.Instead of associative array i'm getting std object:stdClass Object ( [result] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [key] => product_id [value] => 1 ) ...
– Helmsmantest
Nov 6 '17 at 15:12
then cast with(array) $result
after you get the result.
– MagePsycho
Nov 6 '17 at 15:13
Unfortunatelly we can not modify code on the client's side. Is there some other method to get associative array as the result of soap function calling?
– Helmsmantest
Nov 6 '17 at 16:01
Any updates, guys?
– Helmsmantest
Nov 9 '17 at 14:39
add a comment |
Rewrite your class as:
class Gcapiapi implements GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return mixed
*/
public function list($products = NULL)
{
$result = array();
$stockItems = ...
...
foreach($stockItems as $stockItem){
$itemData = array('product_id' => $product->getId(), 'sku' => $productSku, 'qty' => $stockItem->getQty(), 'is_in_stock' => $stockItem->getIsInStock());
$result = $itemData;
}
return $result;
}
}
You can see the change in return type: array
-> mixed
It's strongly advised to use Data interface in such cases even though mixed
will work for you.
Rewrite your class as:
class Gcapiapi implements GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @param string $products
* @return mixed
*/
public function list($products = NULL)
{
$result = array();
$stockItems = ...
...
foreach($stockItems as $stockItem){
$itemData = array('product_id' => $product->getId(), 'sku' => $productSku, 'qty' => $stockItem->getQty(), 'is_in_stock' => $stockItem->getIsInStock());
$result = $itemData;
}
return $result;
}
}
You can see the change in return type: array
-> mixed
It's strongly advised to use Data interface in such cases even though mixed
will work for you.
answered Nov 6 '17 at 14:40
MagePsychoMagePsycho
3,24711944
3,24711944
In this case.Instead of associative array i'm getting std object:stdClass Object ( [result] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [key] => product_id [value] => 1 ) ...
– Helmsmantest
Nov 6 '17 at 15:12
then cast with(array) $result
after you get the result.
– MagePsycho
Nov 6 '17 at 15:13
Unfortunatelly we can not modify code on the client's side. Is there some other method to get associative array as the result of soap function calling?
– Helmsmantest
Nov 6 '17 at 16:01
Any updates, guys?
– Helmsmantest
Nov 9 '17 at 14:39
add a comment |
In this case.Instead of associative array i'm getting std object:stdClass Object ( [result] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [key] => product_id [value] => 1 ) ...
– Helmsmantest
Nov 6 '17 at 15:12
then cast with(array) $result
after you get the result.
– MagePsycho
Nov 6 '17 at 15:13
Unfortunatelly we can not modify code on the client's side. Is there some other method to get associative array as the result of soap function calling?
– Helmsmantest
Nov 6 '17 at 16:01
Any updates, guys?
– Helmsmantest
Nov 9 '17 at 14:39
In this case.Instead of associative array i'm getting std object:stdClass Object ( [result] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [key] => product_id [value] => 1 ) ...
– Helmsmantest
Nov 6 '17 at 15:12
In this case.Instead of associative array i'm getting std object:stdClass Object ( [result] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [key] => product_id [value] => 1 ) ...
– Helmsmantest
Nov 6 '17 at 15:12
then cast with
(array) $result
after you get the result.– MagePsycho
Nov 6 '17 at 15:13
then cast with
(array) $result
after you get the result.– MagePsycho
Nov 6 '17 at 15:13
Unfortunatelly we can not modify code on the client's side. Is there some other method to get associative array as the result of soap function calling?
– Helmsmantest
Nov 6 '17 at 16:01
Unfortunatelly we can not modify code on the client's side. Is there some other method to get associative array as the result of soap function calling?
– Helmsmantest
Nov 6 '17 at 16:01
Any updates, guys?
– Helmsmantest
Nov 9 '17 at 14:39
Any updates, guys?
– Helmsmantest
Nov 9 '17 at 14:39
add a comment |
Once you've received the response from Magento, you can use the following snippet to convert the SOAP object into a PHP associative array:
<?php
$response = simplexml_load_string($soapResponse->any);
$response = json_decode(json_encode($response), true);
Assuming that $soapResponse->any
has this content:
<data count="1" count_available="1">
<row id="1">
<sku>1830DMG</sku>
<price>74.06</price>
<stockid>519745</stockid>
</row>
</data>
You'll get an array which looks like this:
$ php -f apitest3.php
array(2) {
["@attributes"]=>
array(2) {
["count"]=>
string(1) "1"
["count_available"]=>
string(1) "1"
}
["row"]=>
array(4) {
["@attributes"]=>
array(1) {
["id"]=>
string(1) "1"
}
["sku"]=>
string(7) "1830DMG"
["price"]=>
string(5) "74.06"
["stockid"]=>
string(6) "519745"
}
}
add a comment |
Once you've received the response from Magento, you can use the following snippet to convert the SOAP object into a PHP associative array:
<?php
$response = simplexml_load_string($soapResponse->any);
$response = json_decode(json_encode($response), true);
Assuming that $soapResponse->any
has this content:
<data count="1" count_available="1">
<row id="1">
<sku>1830DMG</sku>
<price>74.06</price>
<stockid>519745</stockid>
</row>
</data>
You'll get an array which looks like this:
$ php -f apitest3.php
array(2) {
["@attributes"]=>
array(2) {
["count"]=>
string(1) "1"
["count_available"]=>
string(1) "1"
}
["row"]=>
array(4) {
["@attributes"]=>
array(1) {
["id"]=>
string(1) "1"
}
["sku"]=>
string(7) "1830DMG"
["price"]=>
string(5) "74.06"
["stockid"]=>
string(6) "519745"
}
}
add a comment |
Once you've received the response from Magento, you can use the following snippet to convert the SOAP object into a PHP associative array:
<?php
$response = simplexml_load_string($soapResponse->any);
$response = json_decode(json_encode($response), true);
Assuming that $soapResponse->any
has this content:
<data count="1" count_available="1">
<row id="1">
<sku>1830DMG</sku>
<price>74.06</price>
<stockid>519745</stockid>
</row>
</data>
You'll get an array which looks like this:
$ php -f apitest3.php
array(2) {
["@attributes"]=>
array(2) {
["count"]=>
string(1) "1"
["count_available"]=>
string(1) "1"
}
["row"]=>
array(4) {
["@attributes"]=>
array(1) {
["id"]=>
string(1) "1"
}
["sku"]=>
string(7) "1830DMG"
["price"]=>
string(5) "74.06"
["stockid"]=>
string(6) "519745"
}
}
Once you've received the response from Magento, you can use the following snippet to convert the SOAP object into a PHP associative array:
<?php
$response = simplexml_load_string($soapResponse->any);
$response = json_decode(json_encode($response), true);
Assuming that $soapResponse->any
has this content:
<data count="1" count_available="1">
<row id="1">
<sku>1830DMG</sku>
<price>74.06</price>
<stockid>519745</stockid>
</row>
</data>
You'll get an array which looks like this:
$ php -f apitest3.php
array(2) {
["@attributes"]=>
array(2) {
["count"]=>
string(1) "1"
["count_available"]=>
string(1) "1"
}
["row"]=>
array(4) {
["@attributes"]=>
array(1) {
["id"]=>
string(1) "1"
}
["sku"]=>
string(7) "1830DMG"
["price"]=>
string(5) "74.06"
["stockid"]=>
string(6) "519745"
}
}
answered Jun 15 '18 at 10:34
ProcessEightProcessEight
7121417
7121417
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f200224%2fmagento-2-make-soap-response-as-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown