Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

API issues in 1.1.6
 
cclohman
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2008-06-16
Los Angeles
 

Hi,

I’m trying to test out the api functionality on my new 1.1.6 install.  My installation has all of the recommended items, according to the Magento docs, as well as soap installed (I’ve seen other posts where that was the issue).

I have a very simple script that is trying to connect to the store and display a list of the products (of which there is exactly 1).

Here’s my sample code:

$proxy = new SoapClient('http://dev5.tridian.com/magentostore/magento/api/?wsdl');

    
$sessionId $proxy->login('api_user''xxxxxx');

    
$products $proxy->call($sessionId'product.list');
    
    
var_dump($products);

Here’s what shows up in the error log:

[Fri Sep 19 13:00:02 2008] [error] [client 24.199.0.38] PHP Fatal error:  SOAP-ERRORParsing WSDLCouldn't find <definitions> in 'http://dev5.tridian.com/magentostore/magento/api/?wsdl' in /www/sites/dev5.tridian.com/files/html/api_test.php on line 15
[Fri Sep 19 13:00:02 2008] [error] [client 24.199.0.38] PHP Fatal error:  Uncaught SoapFault exception[WSDL] SOAP-ERRORParsing WSDLCouldn't find <definitions> in 'http://dev5.tridian.com/magentostore/magento/api/?wsdl' in /www/sites/dev5.tridian.com/files/html/api_test.php:15
[Fri Sep 19 13:00:02 2008] [error] [client 24.199.0.38] Stack trace:
[Fri Sep 19 13:00:02 2008] [error] [client 24.199.0.38] #0 /www/sites/dev5.tridian.com/files/html/api_test.php(15): SoapClient->SoapClient('http://dev5.tri...')
[Fri Sep 19 13:00:02 2008] [error] [client 24.199.0.38] #1 {main}
[Fri Sep 19 13:00:02 2008] [error] [client 24.199.0.38]   thrown in /www/sites/dev5.tridian.com/files/html/api_test.php on line 15

Can anyone help me understand what this means?  It seems like it’s tripping over something in creating the wsdl. 

Lastly, if I try to hit the wsdl url directly (http://dev5.tridian.com/magentostore/magento/api/?wsdl), I get the following stack trace:

I’ve also tried the other variations of the soap url as posted by others in the forum (using index.php, adding default folder, etc)

WarningSimpleXMLElement::addAttribute() [function.SimpleXMLElement-addAttribute]Attribute name and value are required  in /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php on line 387
Trace
:
#0 [internal function]: mageCoreErrorHandler(2, 'SimpleXMLElemen...', '/www/sites/dev5...', 387, Array)
#1 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(387): SimpleXMLElement->addAttribute('descr', '')
#2 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(409): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#3 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(409): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#4 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(409): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#5 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(409): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#6 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(342): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#7 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Config.php(592): Varien_Simplexml_Element->extend(Object(Mage_Core_Model_Config_Element), true)
#8 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Api/Model/Config.php(78): Varien_Simplexml_Config->extend(Object(Mage_Core_Model_Config_Base), true)
#9 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Api/Model/Config.php(49): Mage_Api_Model_Config->_construct()
#10 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Core/Model/Config.php(721): Mage_Api_Model_Config->__construct(Array)
#11 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/Mage.php(314): Mage_Core_Model_Config->getModelInstance('api/config', Array)
#12 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/Mage.php(328): Mage::getModel('api/config', Array)
#13 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Api/Model/Server.php(45): Mage::getSingleton('api/config')
#14 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Api/controllers/IndexController.php(41): Mage_Api_Model_Server->init(Object(Mage_Api_IndexController), 'soap')
#15 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Core/Controller/Varien/Action.php(349): Mage_Api_IndexController->indexAction()
#16 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(163): Mage_Core_Controller_Varien_Action->dispatch('index')
#17 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Core/Controller/Varien/Front.php(174): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#18 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/Mage.php(447): Mage_Core_Controller_Varien_Front->dispatch()
#19 /www/sites/dev5.tridian.com/files/html/magentostore/magento/index.php(52): Mage::run()
#20 {main}

Is anyone else having the same issue?  Anyone know what the problem here is?

Thanks so much.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mad Martigan
Member
 
Total Posts:  44
Joined:  2008-08-17
Medford, OR USA
 

Here is the connection routine we use for soap access. 

I noticed that your URL is different than what we use.  This might help....

$host'example.com/magento';
$proxy= new SoapClient('http://'.$host.'/index.php/api/soap/?wsdl');
 
// Can be added in Magento-Admin -> Web Services
$apiuser'username';
$apikey 'password';

try 
{
  $sessionId
$proxy->login($apiuser$apikey);
catch (Exception $e{
  
echo "==> Error: ".$e->getMessage();
  exit();
}

Hope that helps.

 
Magento Community Magento Community
Magento Community
Magento Community
 
cclohman
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2008-06-16
Los Angeles
 

Thanks for the suggestion MM, but after trying the modified url, the same issue remains. 

Plugging this url into the browser

http://dev5.tridian.com/magentostore/magento/index.php/api/soap/?wsdl

still yields

WarningSimpleXMLElement::addAttribute() [function.SimpleXMLElement-addAttribute]Attribute name and value are required  in /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php on line 387
Trace
:
#0 [internal function]: mageCoreErrorHandler(2, 'SimpleXMLElemen...', '/www/sites/dev5...', 387, Array)
#1 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(387): SimpleXMLElement->addAttribute('descr', '')
#2 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(409): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#3 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(409): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#4 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(409): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#5 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(409): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#6 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(342): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#7 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Config.php(592): Varien_Simplexml_Element->extend(Object(Mage_Core_Model_Config_Element), true)
#8 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Api/Model/Config.php(78): Varien_Simplexml_Config->extend(Object(Mage_Core_Model_Config_Base), true)
#9 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Api/Model/Config.php(49): Mage_Api_Model_Config->_construct()
#10 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Core/Model/Config.php(721): Mage_Api_Model_Config->__construct(Array)
#11 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/Mage.php(314): Mage_Core_Model_Config->getModelInstance('api/config', Array)
#12 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/Mage.php(328): Mage::getModel('api/config', Array)
#13 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Api/Model/Server.php(45): Mage::getSingleton('api/config')
#14 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Api/controllers/SoapController.php(41): Mage_Api_Model_Server->init(Object(Mage_Api_SoapController))
#15 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Core/Controller/Varien/Action.php(349): Mage_Api_SoapController->indexAction()
#16 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(163): Mage_Core_Controller_Varien_Action->dispatch('index')
#17 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Core/Controller/Varien/Front.php(174): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#18 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/Mage.php(447): Mage_Core_Controller_Varien_Front->dispatch()
#19 /www/sites/dev5.tridian.com/files/html/magentostore/magento/index.php(52): Mage::run()
#20 {main}

Anyone else out there having a problem with this?  Please help!

 
Magento Community Magento Community
Magento Community
Magento Community
 
cclohman
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2008-06-16
Los Angeles
 

Putting a try catch around the SoapClient statment yields

Error: SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn’t load from ‘http://dev5.tridian.com/magentostore/magento/index.php/api/soap/?wsdl’ in /www/sites/dev5.tridian.com/files/html/api_test.php:14
Stack trace:
#0 /www/sites/dev5.tridian.com/files/html/api_test.php(14): SoapClient->SoapClient(’http://dev5.tri...’)
#1 {main}

Apparently it’s not parsing the WSDL properly.  Any idea why?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mad Martigan
Member
 
Total Posts:  44
Joined:  2008-08-17
Medford, OR USA
 

Hmm, not sure really, but to be thorough, I’ll go through the steps I used to get it working.

1) Create an API user in Magento by going to System->Web Services->Users and add a new user to your system
2) Make sure the new user is assigned to the “admin” group or has adequate permissions.
3) Make sure the “New Api Key” is set to the password you want to use for the new user.
4) Save the following code in a file (for example test.php)
5) Run the code by executing php ./test.php at the command line (for Unix systems)

The resulting output gave me a list of all products in our store presented in a multi-dimensional array.

My guess is that there is something going on with your API user and/or password.

<?php 

// initialize magento environment for 'default' store 
require_once('/path/to/magento/app/Mage.php'); 
Mage::app('default');
 
$host'localhost/magento';
$proxy= new SoapClient('http://'.$host.'/index.php/api/soap/?wsdl');
 
// Can be added in Magento-Admin -> Web Services
$apiuser'replace_with_API-USER_username';
$apikey 'replace_with_API-USER_password';

try 
{
  $sessionId
$proxy->login($apiuser$apikey);
catch (Exception $e{
  
echo "==> Error: ".$e->getMessage();
  exit();
}
$products 
$proxy->call($sessionId'product.list');
var_dump($products);

?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
cclohman
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2008-06-16
Los Angeles
 

MM,

Thanks for those tips.  I went into my admin and saw that I didn’t have a role assigned to my “api” user.  When I try to create a role for the api user via System -> Web Services -> Roles -> Add New Role, I get the SAME error as when I try to hit the API url for the WSDL.

What are your thoughts on that? 

I’m also curious why you call Mage::app(’default’) before your SOAP stuff.  If the PHP script was running remotely, you wouldn’t do this, right?

WarningSimpleXMLElement::addAttribute() [function.SimpleXMLElement-addAttribute]Attribute name and value are required  in /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php on line 387
Trace
:
#0 [internal function]: mageCoreErrorHandler(2, 'SimpleXMLElemen...', '/www/sites/dev5...', 387, Array)
#1 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(387): SimpleXMLElement->addAttribute('descr', '')
#2 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(410): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#3 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(410): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#4 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(410): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#5 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(410): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#6 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Element.php(342): Varien_Simplexml_Element->extendChild(Object(Mage_Core_Model_Config_Element), true)
#7 /www/sites/dev5.tridian.com/files/html/magentostore/magento/lib/Varien/Simplexml/Config.php(592): Varien_Simplexml_Element->extend(Object(Mage_Core_Model_Config_Element), true)
#8 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Api/Model/Config.php(78): Varien_Simplexml_Config->extend(Object(Mage_Core_Model_Config_Base), true)
#9 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Api/Model/Config.php(49): Mage_Api_Model_Config->_construct()
#10 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Core/Model/Config.php(721): Mage_Api_Model_Config->__construct(Array)
#11 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/Mage.php(314): Mage_Core_Model_Config->getModelInstance('api/config', Array)
#12 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/Mage.php(328): Mage::getModel('api/config', Array)
#13 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Api/Model/Roles.php(70): Mage::getSingleton('api/config')
#14 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Api/Model/Roles.php(52): Mage_Api_Model_Roles->_buildResourcesArray()
#15 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Adminhtml/controllers/Api/RoleController.php(84): Mage_Api_Model_Roles->getResourcesList()
#16 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Core/Controller/Varien/Action.php(349): Mage_Adminhtml_Api_RoleController->editRoleAction()
#17 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Core/Controller/Varien/Router/Admin.php(143): Mage_Core_Controller_Varien_Action->dispatch('editrole')
#18 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/code/core/Mage/Core/Controller/Varien/Front.php(174): Mage_Core_Controller_Varien_Router_Admin->match(Object(Mage_Core_Controller_Request_Http))
#19 /www/sites/dev5.tridian.com/files/html/magentostore/magento/app/Mage.php(447): Mage_Core_Controller_Varien_Front->dispatch()
#20 /www/sites/dev5.tridian.com/files/html/magentostore/magento/index.php(52): Mage::run()
#21 {main}
 
Magento Community Magento Community
Magento Community
Magento Community
 
cclohman
Sr. Member
 
Avatar
Total Posts:  82
Joined:  2008-06-16
Los Angeles
 

Problem:

So my problem was being caused by the following piece of code in /lib/Varien/SimpleXML/Element.php, snippet is below starting on line 382:

$targetChild $this->addChild($sourceName$source->xmlentities());
            
$targetChild->setParent($this);

            foreach (
$source->attributes() as $key=>$value{
                $targetChild
->addAttribute($key$this->xmlentities($value)); // problem is right here!!
            
}
            
return $this;

Solution:

At the top of the extendChild function, I added a line to turn off error reporting.  Since it’s a warning that’s being thrown here, and php execution continues, I figured that maybe if we suppress the error, we might get the functionality back. 

public function extendChild($source$overwrite=false)
    
{
        
// turn off error reporting
        
error_reporting(0);
        
// this will be our new target node
        
$targetChild null;

So far so good, it seems.  After implementing this change to Element.php, I am able to get a proper XML document back when hitting:

http://dev5.tridian.com/magentostore/magento/index.php/api/soap/?wsdl

And, even better, MM’s script to pull products from the db via the api works.

Here’s the final code for my test script.  Note, you do not need to call Mage:app(’default’), so I removed that from the script. 

<?php
$proxy
= new SoapClient('http://yoursite.com/magentostore/magento/index.php/api/soap/?wsdl');
// Can be added in Magento-Admin -> Web Services
$apiuser'your_user_here';
$apikey 'your_key_here';

try 
{
  $sessionId
$proxy->login($apiuser$apikey);
catch (Exception $e{
  
echo "==> Error: ".$e->getMessage();
  exit();
}
$products 
$proxy->call($sessionId'product.list');
var_dump($products);
?>

Lastly, I should probably point out that I have my store installed one directory down (site.com/magentostore/magento) from where most others probably have theirs (site.com/magento).  Just a heads up.

I also reported a problem adding Web services roles here.  This solution seems to address both issues.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top