0

I know similar questions have already been asked, however, I have tried the solutions without any success. I keep getting this error:

Fatal error: Call to a member function asXML() on a non-object in ... on line 188

Here is my code:

$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());

I know that $parsed_xml === FALSE so I know the processing of the XML is not working. I suspect it has something to do with $feedHandle as I was previously receiving an error that said FeedContent in the array $params was empty. I know the xml is formatted correctly as I have printed it out and also directly put it in the required field and it worked fine. Also we used curl-ing in a similar file and it was working fine so I do not think that would be the issue either.

stackUser4444
  • 19
  • 1
  • 4

1 Answers1

0

There are a few things that I'd like to comment on.

  1. file_get_contents returns a string, not a file handle. Internally it opens a file handle and closes it automatically. Calling fclose() is unnecessary and will fail because your $feedHandle variable does not contain a file handle (and as such is a misnomer).
  2. I believe you need to POST XML feeds. I haven't tried using GET, but sending feeds by using a GET request will at least limit the size of what you can send.
  3. You have no error handling whatsoever. You (somehow) know that $parsed_xml is false, but haven't changed your code. Your code should read:

.

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

Actually, you want to know what's going on in the steps before that: You need to check $response from curl_exec() as well. The manual page on curl_exec() says it will return false upon failure.

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());
    }
}

Once you've done that, you should get a way more helpful comment than knowing asXML() not being a member function of false.

Community
  • 1
  • 1
Hazzit
  • 6,782
  • 1
  • 27
  • 46
  • Thanks for your help! No sure how much you know about MWS but I am now getting an error: you must pass a Content-MD5 HTTP header for your feed so we can be sure it was not corrupted – stackUser4444 Jan 09 '15 at 20:53
  • I have not changed anything except for my array $params is now: '$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", 'ContentMd5' => base64_encode(md5(stream_get_contents($feedHandle), true)), //String in proper format 'FeedContent' => $feedHandle, 'MarketplaceId.Id.1'=> "********", 'FeedType' => "_POST_PRODUCT_DATA_");' – stackUser4444 Jan 09 '15 at 20:54
  • I am confused because when I print out the array there appears to be a contentMD5 header – stackUser4444 Jan 09 '15 at 20:54