【问题标题】:Get all activities from a Google+ page and access the content从 Google+ 信息页获取所有活动并访问内容
【发布时间】:2015-03-20 09:49:29
【问题描述】:

我正在尝试使用 Google PHP Api Client 从 G+ 页面获取所有活动:

$service = new \Google_Service_Plus($this->client);
    $activities = $service->activities->listActivities($data['page_id'], 'public');

结果是Google_Service_Plus_Activity 对象的数组。问题是我想访问其中一个活动的内容来搜索字符串,但这是在["modelData":protected]=> array(1) 下作为["content"]=> string(int) "my_string"。是否有可能访问内容?

var_dump($activity):

  object(Google_Service_Plus_Activity)#268 (28) { ["internal_gapi_mappings":protected]=> array(0) { } ["accessType":protected]=> string(23) "Google_Service_Plus_Acl" ["accessDataType":protected]=> string(0) "" ["actorType":protected]=> string(33) "Google_Service_Plus_ActivityActor" ["actorDataType":protected]=> string(0) "" ["address"]=> NULL ["annotation"]=> NULL ["crosspostSource"]=> NULL ["etag"]=> string(57) ""RqKWnRU4WW46-6W3rWhLR9iFZQM/t4CM2-QFWMd8gXogVxXGqNUhGSA"" ["geocode"]=> NULL ["id"]=> string(35) "z133dxyyhuaqf5tqd04cebxrtuykyfc5vi0" ["kind"]=> string(13) "plus#activity" ["locationType":protected]=> string(25) "Google_Service_Plus_Place" ["locationDataType":protected]=> string(0) "" ["objectType":protected]=> string(34) "Google_Service_Plus_ActivityObject" ["objectDataType":protected]=> string(0) "" ["placeId"]=> NULL ["placeName"]=> NULL ["providerType":protected]=> string(36) "Google_Service_Plus_ActivityProvider" ["providerDataType":protected]=> string(0) "" ["published"]=> string(24) "2015-03-20T09:03:51.236Z" ["radius"]=> NULL ["title"]=> string(103) "http://www.company-civ.localhost.com/provider/endpoint/validation_social/google?validate=017f5b8e-cee0..." ["updated"]=> string(24) "2015-03-20T09:03:51.236Z" ["url"]=> string(63) "https://plus.google.com/104620991895987671420/posts/akiK7Yx6eus" ["verb"]=> string(4) "post" ["modelData":protected]=> array(4) { ["actor"]=> array(4) { ["id"]=> string(21) "104620991895987671420" ["displayName"]=> string(27) "Magazin de scule barbatesti" ["url"]=> string(45) "https://plus.google.com/104620991895987671420" ["image"]=> array(1) { ["url"]=> string(98) "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50" } } ["object"]=> array(6) { ["objectType"]=> string(4) "note" ["content"]=> string(264) "http://www.company-civ.localhost.com/provider/endpoint/validation_social/google?validate=017f5b8e-cee0-11e4-8698-448a5b89d5f1" ["url"]=> string(63) "https://plus.google.com/104620991895987671420/posts/akiK7Yx6eus" ["replies"]=> array(2) { ["totalItems"]=> int(0) ["selfLink"]=> string(90) "https://www.googleapis.com/plus/v1/activities/z133dxyyhuaqf5tqd04cebxrtuykyfc5vi0/comments" } ["plusoners"]=> array(2) { ["totalItems"]=> int(0) ["selfLink"]=> string(98) "https://www.googleapis.com/plus/v1/activities/z133dxyyhuaqf5tqd04cebxrtuykyfc5vi0/people/plusoners" } ["resharers"]=> array(2) { ["totalItems"]=> int(0) ["selfLink"]=> string(98) "https://www.googleapis.com/plus/v1/activities/z133dxyyhuaqf5tqd04cebxrtuykyfc5vi0/people/resharers" } } ["provider"]=> array(1) { ["title"]=> string(7) "Google+" } ["access"]=> array(3) { ["kind"]=> string(8) "plus#acl" ["description"]=> string(6) "Public" ["items"]=> array(1) { [0]=> array(1) { ["type"]=> string(6) "public" } } } } ["processed":protected]=> array(0) { } } object(Google_Service_Plus_Activity)#269 (28) { ["internal_gapi_mappings":protected]=> array(0) { } ["accessType":protected]=> string(23) "Google_Service_Plus_Acl" ["accessDataType":protected]=> string(0) "" ["actorType":protected]=> string(33) "Google_Service_Plus_ActivityActor" ["actorDataType":protected]=> string(0) "" ["address"]=> NULL ["annotation"]=> NULL ["crosspostSource"]=> NULL ["etag"]=> string(57) ""RqKWnRU4WW46-6W3rWhLR9iFZQM/Z-Jgsn-qD_7-wIVme1yMD5E2Y6k"" ["geocode"]=> NULL ["id"]=> string(33) "z13pt1n4owmog3peg22lzhyirobmu1ivk" ["kind"]=> string(13) "plus#activity" ["locationType":protected]=> string(25) "Google_Service_Plus_Place" ["locationDataType":protected]=> string(0) "" ["objectType":protected]=> string(34) "Google_Service_Plus_ActivityObject" ["objectDataType":protected]=> string(0) "" ["placeId"]=> NULL ["placeName"]=> NULL ["providerType":protected]=> string(36) "Google_Service_Plus_ActivityProvider" ["providerDataType":protected]=> string(0) "" ["published"]=> string(24) "2015-03-20T08:35:56.611Z" ["radius"]=> NULL ["title"]=> string(103) "http://www.company-civ.localhost.com/provider/endpoint/validation_social/google?validate=1c09b6ce-cedc..." ["updated"]=> string(24) "2015-03-20T08:35:56.611Z" ["url"]=> string(63) "https://plus.google.com/104620991895987671420/posts/Dpn1rWu44Dp" ["verb"]=> string(4) "post" ["modelData":protected]=> array(4) { ["actor"]=> array(4) { ["id"]=> string(21) "104620991895987671420" ["displayName"]=> string(27) "Magazin de scule barbatesti" ["url"]=> string(45) "https://plus.google.com/104620991895987671420" ["image"]=> array(1) { ["url"]=> string(98) "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50" } } ["object"]=> array(6) { ["objectType"]=> string(4) "note" ["content"]=> string(264) "http://www.company-civ.localhost.com/provider/endpoint/validation_social/google?validate=1c09b6ce-cedc-11e4-8c62-448a5b89d5f1" ["url"]=> string(63) "https://plus.google.com/104620991895987671420/posts/Dpn1rWu44Dp" ["replies"]=> array(2) { ["totalItems"]=> int(0) ["selfLink"]=> string(88) "https://www.googleapis.com/plus/v1/activities/z13pt1n4owmog3peg22lzhyirobmu1ivk/comments" } ["plusoners"]=> array(2) { ["totalItems"]=> int(0) ["selfLink"]=> string(96) "https://www.googleapis.com/plus/v1/activities/z13pt1n4owmog3peg22lzhyirobmu1ivk/people/plusoners" } ["resharers"]=> array(2) { ["totalItems"]=> int(0) ["selfLink"]=> string(96) "https://www.googleapis.com/plus/v1/activities/z13pt1n4owmog3peg22lzhyirobmu1ivk/people/resharers" } } ["provider"]=> array(1) { ["title"]=> string(7) "Google+" } ["access"]=> array(3) { ["kind"]=> string(8) "plus#acl" ["description"]=> string(6) "Public" ["items"]=> array(1) { [0]=> array(1) { ["type"]=> string(6) "public" } } } } ["processed":protected]=> array(0) { } } object(Google_Service_Plus_Activity)#272 (28) { ["internal_gapi_mappings":protected]=> array(0) { } ["accessType":protected]=> string(23) "Google_Service_Plus_Acl" ["accessDataType":protected]=> string(0) "" ["actorType":protected]=> string(33) "Google_Service_Plus_ActivityActor" ["actorDataType":protected]=> string(0) "" ["address"]=> NULL ["annotation"]=> NULL ["crosspostSource"]=> NULL ["etag"]=> string(57) ""RqKWnRU4WW46-6W3rWhLR9iFZQM/YjJYiH1WPwlvGXudZ_Hq21fjRLc"" ["geocode"]=> NULL ["id"]=> string(35) "z13eun0r1tikx3lju04cebxrtuykyfc5vi0" ["kind"]=> string(13) "plus#activity" ["locationType":protected]=> string(25) "Google_Service_Plus_Place" ["locationDataType":protected]=> string(0) "" ["objectType":protected]=> string(34) "Google_Service_Plus_ActivityObject" ["objectDataType":protected]=> string(0) "" ["placeId"]=> NULL ["placeName"]=> NULL ["providerType":protected]=> string(36) "Google_Service_Plus_ActivityProvider" ["providerDataType":protected]=> string(0) "" ["published"]=> string(24) "2015-03-20T08:34:31.517Z" ["radius"]=> NULL ["title"]=> string(103) "http://www.company-civ.localhost.com/provider/endpoint/validation_social/google?validate=e51795c8-cedb..." ["updated"]=> string(24) "2015-03-20T08:34:31.517Z" ["url"]=> string(63) "https://plus.google.com/104620991895987671420/posts/iubGf6imv4c" ["verb"]=> string(4) "post" ["modelData":protected]=> array(4) { ["actor"]=> array(4) { ["id"]=> string(21) "104620991895987671420" ["displayName"]=> string(27) "Magazin de scule barbatesti" ["url"]=> string(45) "https://plus.google.com/104620991895987671420" ["image"]=> array(1) { ["url"]=> string(98) "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50" } } ["object"]=> array(6) { ["objectType"]=> string(4) "note" ["content"]=> string(264) "http://www.company-civ.localhost.com/provider/endpoint/validation_social/google?validate=e51795c8-cedb-11e4-88a6-448a5b89d5f1" ["url"]=> string(63) "https://plus.google.com/104620991895987671420/posts/iubGf6imv4c" ["replies"]=> array(2) { ["totalItems"]=> int(0) ["selfLink"]=> string(90) "https://www.googleapis.com/plus/v1/activities/z13eun0r1tikx3lju04cebxrtuykyfc5vi0/comments" } ["plusoners"]=> array(2) { ["totalItems"]=> int(0) ["selfLink"]=> string(98) "https://www.googleapis.com/plus..

【问题讨论】:

  • @Grigor Florin - 正在使用这个:github.com/google/google-api-php-client ?
  • 是的,我使用那个包。
  • 那么我想一个简单的foreach 会为您解决问题。你能补充你的答案var_dump($activities)吗?
  • 否,因为无法访问。您可以访问其他属性,例如标题等。
  • var_dump($activities) 呢?

标签: php arrays api object google-plus


【解决方案1】:

解决办法是(终于找到了):

$service = new \Google_Service_Plus($this->client);
//you can also set range of post (max 100)
$optParams = array('maxResults' => 100);
$activities = $service->activities->listActivities('{PAGE_ID}', 'public',$optParams);

foreach($activities as $activity) {
   $result = $activity->getObject();
}

$result 是一个包含公共属性 content 的对象 所以:

$result->content;

【讨论】:

    【解决方案2】:

    您可以通过以下方式访问内容:$activity->getObject()->content

    【讨论】:

      【解决方案3】:

      我会在黑暗中拍摄一点点。但是你会告诉我这是否有效:

      foreach($activities as $activity) {
          $activityInfo = $service->activities->get($activity["id"]);
          var_dump($activityInfo);
      }
      

      不确定id 是否在数组的这一层,因为var_dump 不是很整洁。但是一个示例 id 是 z133dxyyhuaqf5tqd04cebxrtuykyfc5vi0

      试试这个方法,告诉我它是否有效。 (或者至少我们的方向是正确的)

      问候

      【讨论】:

      • 它不起作用:$activityInfo 结果是具有相同结构的相同对象。
      • 很奇怪。我在这里测试过:developers.google.com/apis-explorer/#p/plus/v1/…content 应该存在于那里。 (根据 Google 的文档)
      • 我对API不熟悉,只能做假设。很高兴您自己找到了解决方案。 @Beniamin Szabo 也应该获得荣誉。
      • 我们是同事(与@Beniamin Szabo),我们从事同一个项目。调试是在他的机器上进行的:)
      • 嘿。太棒了:) 团队合作!很高兴你解决了你的问题。它肯定会帮助其他人。这就是 SO 中的理念,人们共同努力解决问题。我没有贡献,但我尝试了。问候
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多