Amazon ItemSearch请求是“有效”但使用PHP SoapClient返回没有结果

时间:2012-02-09 18:39:52

标签: php soap amazon-web-services soap-client

我在亚马逊产品广告论坛上发布了此消息,但没有人回应,所以也许有人可以帮助我。

我正在尝试根据关键字显示一些项目,到目前为止,我有这段代码,当它运行时,它表示请求有效但没有返回任何项目,我正在尝试使用PHP SoapClient。有人可以指出我做错了吗?

这是我的代码:

<?php

define("ACCESS_KEY",'###');
define("SECRET_KEY",'###');
define("ASSOCIATE_TAG",'###');

$timeStamp = gmdate("Y-m-d\TH:i:s\Z");
$function = "ItemSearch";
$string = $function.$timeStamp;

$signature = base64_encode(hash_hmac("sha256", $string, SECRET_KEY, True));

$client = new SoapClient('http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl');
//var_dump($client->__getFunctions());

$params = array(
    "Operation"=>$function,
    "AssociateTag"=>ASSOCIATE_TAG,
    "Request"=>array("Keywords"=>"book","ResponseGroup"=>"Medium")
);

$header_arr = array();
$header_arr[] = new SoapHeader( 'http://security.amazonaws.com/doc/2007-01-01/', 'AWSAccessKeyId', ACCESS_KEY );
$header_arr[] = new SoapHeader( 'http://security.amazonaws.com/doc/2007-01-01/', 'Timestamp', $timeStamp );
$header_arr[] = new SoapHeader( 'http://security.amazonaws.com/doc/2007-01-01/', 'Signature', $signature );
$client->__setSoapHeaders($header_arr);

$result=$client->__soapCall($function, array($params));
var_dump($result);

?>

以下是关键字图书

的回复
object(stdClass)#5 (2) {
  ["OperationRequest"]=>
  object(stdClass)#6 (4) {
    ["HTTPHeaders"]=>
    object(stdClass)#7 (1) {
      ["Header"]=>
      object(stdClass)#8 (2) {
        ["Name"]=>
        string(9) "UserAgent"
        ["Value"]=>
        string(26) "PHP-SOAP/5.3.6-13ubuntu3.3"
      }
    }
    ["RequestId"]=>
    string(36) "###"
    ["Arguments"]=>
    object(stdClass)#9 (1) {
      ["Argument"]=>
      object(stdClass)#10 (2) {
        ["Name"]=>
        string(7) "Service"
        ["Value"]=>
        string(19) "AWSECommerceService"
      }
    }
    ["RequestProcessingTime"]=>
    float(0.085933)
  }
  ["Items"]=>
  object(stdClass)#11 (1) {
    ["Request"]=>
    object(stdClass)#12 (2) {
      ["IsValid"]=>
      string(4) "True"
      ["ItemSearchRequest"]=>
      object(stdClass)#13 (2) {
        ["Keywords"]=>
        string(4) "book"
        ["ResponseGroup"]=>
        string(6) "Medium"
      }
    }
  }
}

正如您所看到的,响应中没有实际项目。从文档中可以看出响应中应该有某种TotalResults属性,所以我完全不知道为什么我会得到完全不同的东西。

1 个答案:

答案 0 :(得分:2)

显然问题是你必须指定一个SearchIndex,它不仅默认为All,甚至告诉你它是必需的,它根本不会返回任何结果。

以上替换请求:

$params = array(
    "Operation"=>$function,
    "AssociateTag"=>ASSOCIATE_TAG,
    "Request"=>array("SearchIndex"=>"All","Keywords"=>"book","ResponseGroup"=>"Small")
);