Amazon MWS SubmitFeed - Fatal error: Call to a member function asXML() on a non-object

Amazon MWS SubmitFeed - Fatal error: Call to a member function asXML() on a non-object

我知道已经有人问过类似的问题,但是,我已经尝试了这些解决方案,但没有成功。我不断收到此错误:

致命错误:第 188 行

中的非对象调用成员函数 asXML()

这是我的代码:

$dom->save("productInfo.xml");
$feedHandle = file_get_contents("productInfo.xml");

 $params = array(
'AWSAccessKeyId'=> "*****",
'Action'=>"SubmitFeed",
'SellerId'=> "********",
'SignatureMethod' => "HmacSHA256",
'SignatureVersion'=> "2",
'Timestamp'=> gmdate("Y-m-d\TH:i:s.\0\0\0\Z", time()),
'Version' => "2009-01-01",
'FeedContent' => $feedHandle,//must be a string
'FeedType' => "_POST_PRODUCT_DATA_");

 // Sort the URL parameters
$url_parts = array();
foreach(array_keys($params) as $key)
$url_parts[] = $key . "=" . str_replace('%7E', '~', rawurlencode($params[$key]));
  sort($url_parts);

  // Construct the string to sign
$url_string = implode("&", $url_parts);
$string_to_sign = "GET\nmws.amazonservices.com\n/Feeds/2009-01-01\n" . $url_string;

  // Sign the request
$signature = hash_hmac("sha256", $string_to_sign, "******", TRUE);

  // Base64 encode the signature and make it URL safe
$signature = urlencode(base64_encode($signature));

$url = "https://mws.amazonservices.com/Feeds/2009-01-01" . '?' . $url_string . "&Signature=" . $signature;

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  $response = curl_exec($ch);

  $parsed_xml = simplexml_load_string($response); 

  @fclose($feedHandle);

  Header('Content-type: text/xml');
  print($parsed_xml->asXML());

我知道 $parsed_xml === FALSE 所以我知道 XML 的处理不工作。我怀疑它与 $feedHandle 有关,因为我之前收到一条错误消息,指出数组 $params 中的 FeedContent 为空。我知道 xml 的格式正确,因为我已将其打印出来并直接将其放入必填字段并且工作正常。我们还在类似的文件中使用了 curl-ing,它工作正常,所以我认为这也不是问题。

有几件事我想发表评论。

  1. file_get_contents returns 是一个字符串,不是文件句柄。它在内部打开一个文件句柄并自动关闭它。调用 fclose() 是不必要的,并且会失败,因为您的 $feedHandle 变量 包含文件句柄(因此用词不当)。
  2. 我相信您需要 POST XML 供稿。我没有尝试使用 GET,但使用 GET 请求发送提要至少会 limit the size of what you can send.
  3. 你没有任何错误处理。您(不知何故)知道 $parsed_xmlfalse,但没有更改您的代码。您的代码应为:

.

if (!$parsed_xml) {
  echo "no XML";
} else {
  Header('Content-type: text/xml');
  print($parsed_xml->asXML());
}

实际上,您想知道之前的步骤发生了什么:您还需要从 curl_exec() 检查 $responsemanual page on curl_exec() 表示它会在失败时 return false

if (!$response) {
    echo "curl_exec() failed with the following error: ".curl_error($ch);
} else {
    $parsed_xml = simplexml_load_string($response); 
    if (!$parsed_xml) {
      echo "unable to parse response as XML: ".$response;
    } else {
      Header('Content-type: text/xml');
      print($parsed_xml->asXML());
    }
}

一旦你这样做了,你应该得到比知道 asXML() 不是 false.

的成员函数更有帮助的评论