我正在编写一个与公司SOAP API接口的php应用程序。我正在对其中一个API函数进行调用并返回一个对象。但是,当我尝试在其中一个对象变量上使用foreach时,我收到错误:
警告:在......
中为foreach()提供的参数无效
我的代码:
// 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>");
}
}
这是返回的对象:
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] =>
)
)
以下是gettype调用显示的内容:
TYPE = NULL
答案 0 :(得分:3)
您正在使用不同的对象名称。在foreach中,您有GetProgramSemesterCategoryList,但该对象具有GetProgramSemesterCategoryListResult。注意最后的“结果”。