stdClass的对象和foreach循环 - 运动监测API [英] stdClass object and foreach loops - campaign monitor api
问题描述
我目前竞选显示器API中工作。
下面是我的code:
require_once'../../csrest_general.php';$ AUTH =阵列('API_KEY'=>'xxxxxxxxxxxxxxxxxxxxxxxxx');
$裹=新CS_REST_General($ AUTH);$结果= $ wrap-> get_clients();回声/ API / V3 /客户\\ n&LT的结果; BR />中;
如果($ result-> was_successful()){
回声得到客户\\ n< BR />< pre>中;
后续代码var_dump($ result->响应);
}其他{
回声失败,code'$ result-方式> http_status_ code\\ n< BR />< pre>中。
后续代码var_dump($ result->响应);
}
呼应'< / pre>';
这输出如下:
阵列(2){
[0] =>
对象(stdClass的)#5(2){
[客户端ID] =>
串(32)xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[名称] =>
串(12)XXXXXXXXXXXX
}
[1] =>
对象(stdClass的)#6(2){
[客户端ID] =>
串(32)xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[名称] =>
串(15)XXXXXXXXXXXX
}}
我要如何去把这个变成一个foreach循环?当我尝试以下方法:
的foreach($结果为$ result->响应){
回声$结果;
}
我得到这个错误:
开捕致命错误:类CS_REST_Wrapper_Result的对象不能转换为字符串
块引用>解决方案感谢马克·巴特勒回答这一点,所以我加了他的解决方案:
的foreach($ result->响应,$输入){回声$入门>的ClientID; }
在一般条款,让在运动监测结果的内容需要您访问响应 - >对象的部分结果从方法返回。不幸的是,这是不是从API或文档中这往往只是'print_var的对象从方法调用返回的清晰。
例如,在列表中列出的用户:
require_once'/csrest_lists.php';
$ SLIST =新CS_REST_Lists(YOUR_CM_LIST_ID,YOUR_CM_ACCT_KEY);
$ sSubscribers = $ sList-> get_active_subscribers() - GT;&响应 - GT;的结果; //返回结果
的foreach($ sSubscribers为$ S){
回声$ S-> EmailAddress的。 \\ t的。 $ S->名称。 \\ t的。 $ S->状态。 \\ n;
}希望这是有用的人 - 感谢danyo的问题 - 很少有这些对SO的时刻。
皮特I am currently working within the campaign monitor api.
Here is my code:
require_once '../../csrest_general.php'; $auth = array('api_key' => 'xxxxxxxxxxxxxxxxxxxxxxxxx'); $wrap = new CS_REST_General($auth); $result = $wrap->get_clients(); echo "Result of /api/v3/clients\n<br />"; if($result->was_successful()) { echo "Got clients\n<br /><pre>"; var_dump($result->response); } else { echo 'Failed with code '.$result->http_status_code."\n<br /><pre>"; var_dump($result->response); } echo '</pre>';
This outputs the following:
array(2) { [0]=> object(stdClass)#5 (2) { ["ClientID"]=> string(32) "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ["Name"]=> string(12) "xxxxxxxxxxxx" } [1]=> object(stdClass)#6 (2) { ["ClientID"]=> string(32) "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ["Name"]=> string(15) "xxxxxxxxxxxx" } }
How do i go about putting this into a foreach loop? when i try the following:
foreach ($result as $result->response) { echo $result; }
i get this error:
Catchable fatal error: Object of class CS_REST_Wrapper_Result could not be converted to string
解决方案Credit to Mark Butler for answering this, so I've added his solution:
foreach($result->response as $entry) { echo $entry->ClientID; }
In general terms, getting at the contents of Campaign Monitor results requires you to access the response->Results part of the object returned from the method. Unfortunately, this is not clear from the API or documentation which tends to just 'print_var' the objects returned from the method calls.
For example, listing subscribers in a list:
require_once '/csrest_lists.php'; $sList = new CS_REST_Lists(YOUR_CM_LIST_ID,YOUR_CM_ACCT_KEY); $sSubscribers = $sList->get_active_subscribers()->response->Results; // return results foreach($sSubscribers as $s) { echo $s->EmailAddress . "\t" . $s->Name . "\t" . $s->State . "\n"; }
Hope this is useful to people - thanks danyo for the question - there are few of these on SO at the moment. Pete
这篇关于stdClass的对象和foreach循环 - 运动监测API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!