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.

Draken
  • 3,134
  • 13
  • 34
  • 54
stackUser4444
  • 19
  • 1
  • 4
  • "I have tried using var_dump but did not get True or False returned" - what *did* you get? – IMSoP Jan 07 '15 at 21:26
  • Nothing was printed out to the page or appeared in the view-source – stackUser4444 Jan 07 '15 at 21:27
  • Ah, were you putting `var_dump` around the expression that is erroring, where you have `print()` in the example above? That would mean running it after the fatal error, which won't work. If you try `var_dump?($parsed_xml)`, you'll probably find you have `false` rather than a `SimpleXMLElement` object. In which case, [turn up your error reporting](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851) and make sure you don't have [`libxml_use_internal_errors`](http://php.net/libxml_use_internal_errors) set to true somewhere. – IMSoP Jan 07 '15 at 21:31
  • The error reported on the page says: This page contains the following errors: error on line 2 at column 1: Document is empty Below is a rendering of the page up to the first error. – stackUser4444 Jan 07 '15 at 21:51
  • Also I have confirmed that var_dump returns false. And on the page source it says: bool(false) Fatal error: Call to a member function asXML() on a non-object in W:\wwwroot\Megan\src\amazonFeedTesting.php on line 193 – stackUser4444 Jan 07 '15 at 21:55

2 Answers2

3

The error you are getting suggests that the simplexml_load_string function call failed. It only returns an object if the processing of the XML file was successful, otherwise, it returns a boolean FALSE. Try checking if $parsed_xml === FALSE. If it is returning a FALSE, try checking the XML file itself.

In fact, since you are curl-ing for the file itself, check that all of the fields were set correctly and that the URL you are using is correct by printing it out.

zvava
  • 101
  • 1
  • 3
  • 14
EmmanuelG
  • 1,051
  • 9
  • 14
  • 1
    Also, try setting your error reporting level to show warnings; according to [the documentation for `simplexml_load_string`](http://php.net/manual/en/function.simplexml-load-string.php), which contains all this information, the function "Produces an E_WARNING error message for each error found in the XML data." – sgress454 Jan 08 '15 at 02:56
-1

use try catch, and instead of using Exception class, try Throwable class

Example :-

try {
    $x = false;
    var_dump($x->asXML());
} catch(Throwable $e) {
    echo $e->getMessage();
}
  • @NicoHaase this code always catches exception, as class Exception is under class Throwable in hierarchy. This approach worked for me in my Laravel application. PS. you also use eval(). Then you can have control on output properly (thats what laravel does) – Smruti Ranjan Mar 30 '18 at 14:45