0

I have an XML File for an API. The API responses everytime "Errormesage: Expected argument of type "string", "array" given".

$SimpleXML_loaded_File = simplexml_load_file("http://graphics.edc-internet.nl/b2b_feed.php?key=tc62te28wt3e2t73ctr9c1cw42601337&sort=xml&type=xml&lang=de");

foreach ($SimpleXML_loaded_File->product as $product) {

  /*$client->post('articles', array(
      'name' => $product->titel,
      'taxId' => 1,
      'supplier' => $product->merk,
      'mainDetail' => array(
          'number' => $product->artikelnummer
      )
  ));*/

echo "<pre>";
var_dump($product->artikelnummer);
echo "</pre>";

}

If i var_dump the Output i dont get a string i`m getting the object

object(SimpleXMLElement)#6 (1) {
  [0]=>
  string(7) "0633178"
}

How do i get only the string ?

user3633186
  • 1,510
  • 2
  • 11
  • 21
  • [`var_dump()`](http://php.net/var_dump) is not asking for a string value specifically, so you might just have chosen the wrong function. You might haven been looking for [`strval()`](http://php.net/strval) instead. Or many of the other string functions in PHP. – hakre Mar 06 '15 at 22:44

2 Answers2

2

SimpleXML is simple for a reason. When echoing an element, it automatically converts the object to a string. You can also explicitly cast it to one:

echo (string)$product->artikelnummer;
silkfire
  • 24,585
  • 15
  • 82
  • 105
  • But why do i get the error message:Errormesage: Expected argument of type "string", "array" given. As i said i get the value with $product->artikelnummer; But the API says it`s an array. – user3633186 Mar 05 '15 at 08:49
  • @user3633186 You have to cast your `number` parameter to a string: `'number' => (string)$product->artikelnummer` – silkfire Mar 05 '15 at 09:21
0

This issue is related to type casting. Please check.

echo (string)$product->artikelnummer;