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

Using SOAP with API - How to Start
 
ptjedi
Sr. Member
 
Avatar
Total Posts:  250
Joined:  2008-03-12
Porto, Portugal
 

Hi guys,

With the new API that came with 1.1, I am starting to sketch a few needs that can be (in theory) finnaly accomplished. However, I have ran into the simple problem of getting the system starting so that I can start programming with the API.

I have followed Pixeldreamz’s Tutorial but the error:  “==> Error: Invalid api path.” always appear. I am using XAMPP in Windows, and running Magento 1.1 Beta.

My code test.php

<?php

$host
'localhost/magento11b';
$client= new SoapClient('http://'.$host.'/index.php/api/soap/?wsdl');

// Can be added in Magento-Admin -> Web Services
$apiuser"user";
$apikey "pass";

try 
{
  $sess_id
$client->login($apiuser$apikey);
  
$result$client->call($sess_id‘catalog_category.tree’, array());
catch (Exception $e{
  
echo "==> Error: ".$e->getMessage();
  exit();
}

foreach ($result['children'as $child{
  
echo "–> ".$child['name']." (id:".$child['category_id'].")\n";
}
?>

Info on phpinfo();

soap
Soap Client     enabled
Soap Server     enabled

Directive    Local Value    Master Value
soap
.wsdl_cache    1    1
soap
.wsdl_cache_dir    C:\xampp\tmp    C:\xampp\tmp
soap
.wsdl_cache_enabled    1    1
soap
.wsdl_cache_limit    5    5
soap
.wsdl_cache_ttl    86400    86400

Do anyone have a clue of what is going on and how can I surpass this itching problem? Thanks a lot in advance.

 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

I had the same problem too.

First of all, you need the latest 1.1-trunk beta release which can be acquired through SVN.  The beta release that comes from the Magento Connect wont work.

You need to add this to the beginning of the code.

<?php

// initialize magento environment for 'default' store 
require_once('/***Your Magento Directory***/app/Mage.php'); 
Mage::app('default');

I’m still trying to figure out all the API, the documentation is incomplete.  For example, the whole category API is not there (the links are broken).

I need to figure out how to assign new products that are created to the appropriate categories and also websites.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ptjedi
Sr. Member
 
Avatar
Total Posts:  250
Joined:  2008-03-12
Porto, Portugal
 

Thank you very much. It seems that the Beta (downloadable) is working as well (don’t know if everything is, though) but I managed to get this example working! Those two initial lines were in fact missing wink Thanks again!

 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

Glad I could help.

Have you had any sucess printing out the category tree with the code:

foreach ($result['children'as $child{
  
echo "–> ".$child['name']." (id:".$child['category_id'].")\n";
}

I only get one line from that.

I’m thinking I need some kind of recursion function, but right now my programming skills are very rusty (esp in PHP) and I’m not sure how to traverse the tree.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ptjedi
Sr. Member
 
Avatar
Total Posts:  250
Joined:  2008-03-12
Porto, Portugal
 

All I can get is the list of the first level categories right below Root. No subcategories. Don’t know if we need to hardcode some recursive method, which is stupid because if the API function is called “tree” it should be able to reach every and single level within our category list.

 
Magento Community Magento Community
Magento Community
Magento Community
 
cgalpin
Jr. Member
 
Total Posts:  11
Joined:  2008-06-25
 

If you dump the response it should show all the data. If it does, then, this code snippet will display the tree. If the data isn’t there, then you need to figure that out first.

# Categories
try {
  $result
$client->call($sess_id'catalog_category.tree', array());
  
showCategory($result);
  
//var_dump($result);
catch (Exception $e{
  
echo "==> Error: " $e->getMessage();
  exit();
}

function showCategory($category$indent=0)
{
    
for ($i=0$i<$indent;$i++) echo " "}
    
echo $category['name'" (".$category['category_id'")\n";
    
    
$indent++;
    foreach (
$category['children'as $child
    
{
      showCategory
($child$indent);
    
}
    $indent
--;
}

hth
charles

 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

Thanks for the code.  I already figured out how to print and search the tree, but your print method is better than mine.  I actually was looking for a way to search the tree for the category_id, so I could use it when creating a product.  But I’m sure it will help others to use it.

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