tags:

views:

34

answers:

1

I am writing a php app that interfaces with a companies SOAP API. I am making a call against one of the APIs functions and receiving an object back. However when I try to use a foreach on one of the objects variables I am receiving the error:

Warning: Invalid argument supplied for foreach() in ...

My code:

// Make Call and get result
$result = $soapClient->GetProgramSemesterCategoryList(array("semesterGuid" => "8725a32b-e671-4386-ae70-969a6d600f31"));

// Output the result
print("<b>Result = ");
print_r($result);
print("</b></br>");

// Make sure not getting an exception
if($result->GetProgramSemesterCategoryListResult->IsException)
{
    print("Error!");
}
else
{
    // Output the type of the CategoryInfo in the object
    print("<b>TYPE: " . gettype($result->GetProgramSemesterCategoryList->Value->CategoryInfo) . "</b></br>");
    foreach($result->GetProgramSemesterCategoryList->Value->CategoryInfo as $category)
    {
        print("<b>Categories for this Semester</b></br>");

        print("<b>CategoryId:</b> " . $category->CategoryId . " - <b>CategoryGuid:</b> "
            . $category->CategoryName . " - <b>CategoryName:</b> " . $category->CategoryDesc
            . " - <b>ModuleFor:</b> " . $category->ModuleFor . "</br></br>");
    }
}

Here is the object that is being printed returned:

stdClass Object ( 
    [GetProgramSemesterCategoryListResult] => stdClass Object ( 
        [Value] => stdClass Object ( 
            [CategoryInfo] => Array ( 
                [0] => stdClass Object ( 
                    [CategoryId] => 9 
                    [CategoryGuid] => 7299eb49-d75a-4719-ae18-8ce814e99931 
                    [CategoryName] => Adult Swim Lessons 
                    [CategoryDesc] => New Category 
                    [ModuleFor] => PRG_Program_Registration 
                ) 
                [1] => stdClass Object ( 
                    [CategoryId] => 10 
                    [CategoryGuid] => 4117bbaf-7301-40b2-a149-76daca62b748 
                    [CategoryName] => Child Swim Lessons 
                    [CategoryDesc] => New Category 
                    [ModuleFor] => PRG_Program_Registration 
                ) 
            ) 
        ) 
        [IsException] => 
    ) 
) 

Here is what the gettype call shows:

TYPE = NULL
+3  A: 

You are using different object names. In the foreach you have GetProgramSemesterCategoryList, but the object has GetProgramSemesterCategoryListResult. Notice the "Result" at the end.

Brent Baisley