stdClass 对象和 foreach 循环 - 活动监视器 api [英] stdClass object and foreach loops - campaign monitor api
问题描述
我目前正在使用活动监视器 API.
I am currently working within the campaign monitor api.
这是我的代码:
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>';
输出如下:
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"
}
}
如何将其放入 foreach 循环中?当我尝试以下操作时:
How do i go about putting this into a foreach loop? when i try the following:
foreach ($result as $result->response) {
echo $result;
}
我收到此错误:
可捕获的致命错误:CS_REST_Wrapper_Result 类的对象无法转换为字符串
Catchable fatal error: Object of class CS_REST_Wrapper_Result could not be converted to string
推荐答案
感谢 Mark Butler 回答了这个问题,所以我添加了他的解决方案:
Credit to Mark Butler for answering this, so I've added his solution:
foreach($result->response as $entry) { echo $entry->ClientID; }
一般来说,获取 Campaign Monitor 结果的内容需要您访问从该方法返回的对象的 response->Results 部分.不幸的是,这从 API 或文档中并不清楚,它们往往只是print_var"从方法调用返回的对象.
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";
}
希望这对人们有用 - 感谢 danyo 的问题 - 目前关于 SO 的内容很少.皮特
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屋!