如何从响应XML中获取所需的字段

时间:2015-05-27 15:39:00

标签: php xml amazon amazon-mws

我试图从亚马逊那里获取产品,但亚马逊的反应是XML。 如何在不获取其他字段的情况下获取我真正需要的字段。

这是我的代码:

<?php
date_default_timezone_set('UTC');

$url = 'https://mws.amazonservices.com/Products/2011-10-01';

$data = array(
    'Action' => 'ListMatchingProducts', 
    'SellerId' => '************', 
    'MWSAuthToken' => 'amzn.*************************', 
    'AWSAccessKeyId' => '******************', 
    'Timestamp' => date("Y-m-d\TG:i:s\Z"), 
    'Version' => '2011-10-01', 
    'MarketplaceId' => '************', 
    'SignatureVersion' => '2', 
    'SignatureMethod' => 'HmacSHA256', 
    'Query' => 'hunger games'
    ); 

ksort($data);
$QueryString = http_build_query($data,'','&',PHP_QUERY_RFC3986); 

$stringToSign =
    "POST\n".
    "mws.amazonservices.com\n".
    "/Products/2011-10-01\n".
    $QueryString;

$secret = 'secret*******************************';
$signature = hash_hmac('sha256', $stringToSign, $secret, true);

$data['Signature'] = base64_encode($signature);
ksort($data);


$options = array(
'http' => array(
    'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
    'method'  => 'POST',
    'content' => http_build_query($data),
 ),
);

 $context  = stream_context_create($options);
 $result = file_get_contents($url, false, $context);



 $p = xml_parser_create();
 xml_parse_into_struct($p, $result, $vals, $index);
 xml_parser_free($p);
 echo "<PRE>";
 echo "Index array\n";
 print_r($index);
 echo "\nVals array\n";
 print_r($vals);

这是输出...... 伙计帮助我得到我需要的输出,例如productname,ASIN,imageURL ..

[LISTMATCHINGPRODUCTSRESPONSE] => Array
    (
        [0] => 0
        [1] => 777
    )

[LISTMATCHINGPRODUCTSRESULT] => Array
    (
        [0] => 1
        [1] => 773
    )

[PRODUCTS] => Array
    (
        [0] => 2
        [1] => 772
    )

[PRODUCT] => Array
    (
        [0] => 3
        [1] => 61
        [2] => 62
        [3] => 148
        [4] => 149
        [5] => 192
        [6] => 193
        [7] => 246
        [8] => 247
        [9] => 365
        [10] => 366
        [11] => 448
        [12] => 449
        [13] => 498
        [14] => 499
        [15] => 598
        [16] => 599
        [17] => 714
        [18] => 715
        [19] => 771
    )

[IDENTIFIERS] => Array
    (
        [0] => 4
        [1] => 9
        [2] => 63
        [3] => 68
        [4] => 150
        [5] => 155
        [6] => 194
        [7] => 199
        [8] => 248
        [9] => 253
        [10] => 367
        [11] => 372
        [12] => 450
        [13] => 455
        [14] => 500
        [15] => 505
        [16] => 600
        [17] => 605
        [18] => 716
        [19] => 721
    )

[MARKETPLACEASIN] => Array
    (
        [0] => 5
        [1] => 8
        [2] => 64
        [3] => 67
        [4] => 151
        [5] => 154
        [6] => 195
        [7] => 198
        [8] => 249
        [9] => 252
        [10] => 368
        [11] => 371
        [12] => 451
        [13] => 454
        [14] => 501
        [15] => 504
        [16] => 601
        [17] => 604
        [18] => 717
        [19] => 720
    )

[MARKETPLACEID] => Array
    (
        [0] => 6
        [1] => 65
        [2] => 152
        [3] => 196
        [4] => 250
        [5] => 369
        [6] => 452
        [7] => 502
        [8] => 602
        [9] => 718
    )

[ASIN] => Array
    (
        [0] => 7
        [1] => 66
        [2] => 153
        [3] => 197
        [4] => 251
        [5] => 370
        [6] => 453
        [7] => 503
        [8] => 603
        [9] => 719
    )

[ATTRIBUTESETS] => Array
    (
        [0] => 10
        [1] => 49
        [2] => 69
        [3] => 128
        [4] => 156
        [5] => 189
        [6] => 200
        [7] => 243
        [8] => 254
        [9] => 345
        [10] => 373
        [11] => 428
        [12] => 456
        [13] => 495
        [14] => 506
        [15] => 586
        [16] => 606
        [17] => 694
        [18] => 722
        [19] => 763
    )

[NS2:ITEMATTRIBUTES] => Array
    (
        [0] => 11
        [1] => 48
        [2] => 70
        [3] => 127
        [4] => 157
        [5] => 188
        [6] => 201
        [7] => 242
        [8] => 255
        [9] => 344
        [10] => 374
        [11] => 427
        [12] => 457
        [13] => 494
        [14] => 507
        [15] => 585
        [16] => 607
        [17] => 693
        [18] => 723
        [19] => 762
    )

[NS2:ACTOR] => Array
    (
        [0] => 12
        [1] => 13
        [2] => 14
        [3] => 15
        [4] => 16
        [5] => 17
        [6] => 18
        [7] => 158
        [8] => 202
        [9] => 203
        [10] => 204
        [11] => 205
        [12] => 206
        [13] => 207
        [14] => 208
        [15] => 209
        [16] => 210
        [17] => 211
        [18] => 256
        [19] => 257
        [20] => 258
        [21] => 259
        [22] => 260
        [23] => 261
        [24] => 262
        [25] => 263
        [26] => 264
        [27] => 265
        [28] => 458
        [29] => 459
        [30] => 460
        [31] => 461
        [32] => 462
        [33] => 463
        [34] => 464
        [35] => 508
        [36] => 509
        [37] => 510
        [38] => 511
        [39] => 512
        [40] => 513
        [41] => 514
        [42] => 515
        [43] => 516
        [44] => 517
        [45] => 608
        [46] => 609
        [47] => 610
    )

0 个答案:

没有答案