Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

create an object array from xml response

0 views
Skip to first unread message

jr

unread,
Apr 20, 2010, 7:34:32 PM4/20/10
to
I wrote a web service request with XML response. I don't know how to
get the ProductGroup element in the xml into an array for the requests
that will have more than one item. I only need the ProductGroup
value. It is #17 element in the object the last attribute. The "CE"
is the product group. I only need an array of the ProductGroup
elements.
I changed my access code for security.
thanks,
jr
-------
$access_key = "BLJBJFUKMBEXJMMBBDCB";
$parsed_xml=amazon($access_key);
var_dump($parsed_xml);


function amazon($Access_Key,$Item_Id="C111WDLL13"){
$allparameter =

"Service" ."=".
"AWSECommerceService" ."&".
"AWSAccessKeyId" ."=".
$Access_Key ."&".
"Operation" ."=".
"ItemLookup" ."&".
"IdType" ."-".
"ASIN" ."&".
"ItemId" ."=".
$Item_Id ."&".
"Version" ."=".
"2009-01-06" ."&"
;

$request = file_get_contents("http://free.apisigning.com/onca/
xml?".$allparameter);
$root = simplexml_load_string($request);
$data = get_object_vars($root);
var_dump($data);

}

----------response-----------
array(2) {
["OperationRequest"]=>
object(SimpleXMLElement)#2 (3) {
["RequestId"]=>
string(36) "ed0f6f43-d6c7-44e1-8747-a4830b69cd52"
["Arguments"]=>
object(SimpleXMLElement)#4 (1) {
["Argument"]=>
array(8) {
[0]=>
object(SimpleXMLElement)#5 (1) {
["@attributes"]=>
array(2) {
["Name"]=>
string(9) "Operation"
["Value"]=>
string(10) "ItemLookup"
}
}
[1]=>
object(SimpleXMLElement)#6 (1) {
["@attributes"]=>
array(2) {
["Name"]=>
string(7) "Service"
["Value"]=>
string(19) "AWSECommerceService"
}
}
[2]=>
object(SimpleXMLElement)#7 (1) {
["@attributes"]=>
array(2) {
["Name"]=>
string(6) "ItemId"
["Value"]=>
string(10) "C111WDLL13"
}
}
[3]=>
object(SimpleXMLElement)#8 (1) {
["@attributes"]=>
array(1) {
["Name"]=>
string(11) "IdType-ASIN"
}
}
[4]=>
object(SimpleXMLElement)#9 (1) {
["@attributes"]=>
array(2) {
["Name"]=>
string(14) "AWSAccessKeyId"
["Value"]=>
string(20) "BLJBJFUKMBEXJMMBBDCB"
}
}
[5]=>
object(SimpleXMLElement)#10 (1) {
["@attributes"]=>
array(2) {
["Name"]=>
string(9) "Timestamp"
["Value"]=>
string(20) "2010-04-20T23:06:43Z"
}
}
[6]=>
object(SimpleXMLElement)#11 (1) {
["@attributes"]=>
array(2) {
["Name"]=>
string(9) "Signature"
["Value"]=>
string(44) "7XAIy4I2A0k56IjSdQ6m2028hx6yTOBtSoS9hBSdj/o="
}
}
[7]=>
object(SimpleXMLElement)#12 (1) {
["@attributes"]=>
array(2) {
["Name"]=>
string(7) "Version"
["Value"]=>
string(10) "2009-01-06"
}
}
}
}
["RequestProcessingTime"]=>
string(18) "0.0192610000000000"
}
["Items"]=>
object(SimpleXMLElement)#3 (2) {
["Request"]=>
object(SimpleXMLElement)#13 (2) {
["IsValid"]=>
string(4) "True"
["ItemLookupRequest"]=>
object(SimpleXMLElement)#15 (10) {
["Condition"]=>
string(3) "New"
["DeliveryMethod"]=>
string(4) "Ship"
["IdType"]=>
string(4) "ASIN"
["MerchantId"]=>
string(6) "Amazon"
["OfferPage"]=>
string(1) "1"
["ItemId"]=>
string(10) "C111WDLL13"
["ResponseGroup"]=>
string(5) "Small"
["ReviewPage"]=>
string(1) "1"
["ReviewSort"]=>
string(15) "-SubmissionDate"
["VariationPage"]=>
string(3) "All"
}
}
["Item"]=>
object(SimpleXMLElement)#14 (4) {
["ASIN"]=>
string(10) "C111WDLL13"
["DetailPageURL"]=>
string(208) "http://www.amazon.com/Homedics-SC-650-Professional-
Doctors-Scale/dp/C111WDLL13%3FSubscriptionId%3DBLJBJFUKMBEXJMMBBDCB
%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative
%3D165953%26creativeASIN%3DC111WDLL13"
["ItemLinks"]=>
object(SimpleXMLElement)#16 (1) {
["ItemLink"]=>
array(7) {
[0]=>
object(SimpleXMLElement)#18 (2) {
["Description"]=>
string(17) "Technical Details"
["URL"]=>
string(218) "http://www.amazon.com/Homedics-SC-650-
Professional-Doctors-Scale/dp/tech-data/C111WDLL13%3FSubscriptionId
%3DBLJBJFUKMBEXJMMBBDCB%26tag%3Dws%26linkCode%3Dxm2%26camp
%3D2025%26creative%3D386001%26creativeASIN%3DC111WDLL13"
}
[1]=>
object(SimpleXMLElement)#19 (2) {
["Description"]=>
string(20) "Add To Baby Registry"
["URL"]=>
string(204) "http://www.amazon.com/gp/registry/baby/add-
item.html%3Fasin.0%3DC111WDLL13%26SubscriptionId%3DBLJBJFUKMBEXJMMBBDCB
%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative
%3D386001%26creativeASIN%3DC111WDLL13"
}
[2]=>
object(SimpleXMLElement)#20 (2) {
["Description"]=>
string(23) "Add To Wedding Registry"
["URL"]=>
string(207) "http://www.amazon.com/gp/registry/wedding/add-
item.html%3Fasin.0%3DC111WDLL13%26SubscriptionId%3DBLJBJFUKMBEXJMMBBDCB
%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative
%3D386001%26creativeASIN%3DC111WDLL13"
}
[3]=>
object(SimpleXMLElement)#21 (2) {
["Description"]=>
string(15) "Add To Wishlist"
["URL"]=>
string(208) "http://www.amazon.com/gp/registry/wishlist/
add-item.html%3Fasin.0%3DC111WDLL13%26SubscriptionId
%3DBLJBJFUKMBEXJMMBBDCB%26tag%3Dws%26linkCode%3Dxm2%26camp
%3D2025%26creative%3D386001%26creativeASIN%3DC111WDLL13"
}
[4]=>
object(SimpleXMLElement)#22 (2) {
["Description"]=>
string(13) "Tell A Friend"
["URL"]=>
string(173) "http://www.amazon.com/gp/pdp/taf/
C111WDLL13%3FSubscriptionId%3DBLJBJFUKMBEXJMMBBDCB%26tag%3Dws
%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN
%3DC111WDLL13"
}
[5]=>
object(SimpleXMLElement)#23 (2) {
["Description"]=>
string(20) "All Customer Reviews"
["URL"]=>
string(177) "http://www.amazon.com/review/product/
C111WDLL13%3FSubscriptionId%3DBLJBJFUKMBEXJMMBBDCB%26tag%3Dws
%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN
%3DC111WDLL13"
}
[6]=>
object(SimpleXMLElement)#24 (2) {
["Description"]=>
string(10) "All Offers"
["URL"]=>
string(179) "http://www.amazon.com/gp/offer-listing/
C111WDLL13%3FSubscriptionId%3DBLJBJFUKMBEXJMMBBDCB%26tag%3Dws
%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN
%3DC111WDLL13"
}
}
}
["ItemAttributes"]=>
object(SimpleXMLElement)#17 (3) {
["Manufacturer"]=>
string(8) "Homedics"
["ProductGroup"]=>
string(2) "CE"
["Title"]=>
string(42) "Homedics SC-650 Professional Doctors Scale"
}
}
}
}
NULL

Jerry Stuckle

unread,
Apr 20, 2010, 8:56:42 PM4/20/10
to
jr wrote:
> I wrote a web service request with XML response. I don't know how to
> get the ProductGroup element in the xml into an array for the requests
> that will have more than one item. I only need the ProductGroup
> value. It is #17 element in the object the last attribute. The "CE"
> is the product group. I only need an array of the ProductGroup
> elements.
> I changed my access code for security.
> thanks,
> jr
> -------
> $access_key = "BLJBJFUKMBEXJMMBBDCB";
> $parsed_xml=amazon($access_key);
> var_dump($parsed_xml);
>
>

First of all, this only has one product group. So you can't get
multiple items - all you could do is create an array of one item.

You could get the element out if this one with something like:

$parsed_xml['OperationRequest']->'Items->'Item'->'ItemAttributes'->'ProductGroup'

However, this will only work with this specific hierarchy (and I'm not
100% sure about that - it's untested). Any change, and it will fail
completely.

Chances are you're going to have to parse your XML to get the data you
want reliably. You might want to check the Amazon developer information
to see if someone has code already created to do what you want.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

jr

unread,
Apr 20, 2010, 11:42:26 PM4/20/10
to
> jstuck...@attglobal.net
> ==================
Thanks. I don't think it will change since I have the version date.
For some reason I am getting Parse error: syntax error, unexpected
T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or
'$' in /Library/WebServer/Documents/lexsource7.php on line 22 which is
the line you introduced.
I tried it with your line and then I modified it slightly and I get
the same
syntax error. It looks like it should work to me but I'm just getting
started with the api.

$access_key = "BLJBJFUKMBEXJMMBBDCB";
$request=anazon($access_key);
var_dump($request);


function anazon($Access_Key,$Item_Id="C111WDLL13"){
$allparameter =

"Service" ."=".
"AWSECommerceService" ."&".
"AWSAccessKeyId" ."=".
$Access_Key ."&".
"Operation" ."=".
"ItemLookup" ."&".
"IdType" ."-".
"ASIN" ."&".
"ItemId" ."=".
$Item_Id ."&".
"Version" ."=".
"2009-01-06"

;

$response = file_get_contents("http://free.apisigning.com/onca/
xml?".$allparameter);
$parsed_xml = simplexml_load_string($response);

$parsed_xml->'OperationRequest'->'Items'->'Item'->'ItemAttributes'-
>'ProductGroup' ;

return $parsed_xml;
}

Captain Paralytic

unread,
Apr 21, 2010, 5:10:25 AM4/21/10
to
On 21 Apr, 04:42, jr <jlro...@yahoo.com> wrote:
> On Apr 20, 5:56 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>
Try (also untested)

$parsed_xml['OperationRequest']->Items->Item->ItemAttributes-
>ProductGroup

Jerry Stuckle

unread,
Apr 21, 2010, 6:36:20 AM4/21/10
to

Yep, that should work better...

Thanks for the correction, Paul. Too late when I wrote that :).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

jstu...@attglobal.net
==================

jr

unread,
Apr 21, 2010, 10:25:54 AM4/21/10
to

thanks, I got something returned from the function. It needs some
adjustment but it works.

0 new messages