duanqu9279 2016-08-05 08:49
浏览 64

查询Google Search Console API PHP

I am querying the Google Search Console API and getting data back however I cannot get the specific rows.

$fromDate = date('Y-m-d', strtotime('-3 months'));
$toDate = date('Y-m-d', strtotime('-1 day'));

$client = new Google_Client();
$client->setAuthConfigFile(base_path().'/client_id.json');
$client->setAccessToken($cust->user_token);
$client->addScope(Google_Service_Webmasters::WEBMASTERS_READONLY);
$webmaster = new Google_Service_Webmasters($client);
$url = $cust->console_url;
$options = [];

$search = new Google_Service_Webmasters_SearchAnalyticsQueryRequest;
$search->setStartDate( $fromDate );
$search->setEndDate( $toDate );
$search->setDimensions( ['date'] );
$search->setAggregationType( 'auto' );

$console = $webmaster->urlcrawlerrorscounts->query($url, $options)->getCountPerTypes();
dd($console);

The Response;

array:20 [▼
  0 => Google_Service_Webmasters_UrlCrawlErrorCountsPerType {#301 ▼
    #collection_key: "entries"
    #internal_gapi_mappings: []
    +category: "notFound"
    #entriesType: "Google_Service_Webmasters_UrlCrawlErrorCount"
    #entriesDataType: "array"
    +platform: "web"
    #modelData: array:1 [▼
      "entries" => array:1 [▼
        0 => array:2 [▼
          "count" => "64"
          "timestamp" => "2016-08-04T20:15:25.816Z"
        ]
      ]
    ]
    #processed: []
  }
  1 => Google_Service_Webmasters_UrlCrawlErrorCountsPerType {#302 ▶}
  2 => Google_Service_Webmasters_UrlCrawlErrorCountsPerType {#305 ▶}
  3 => Google_Service_Webmasters_UrlCrawlErrorCountsPerType {#306 ▶}
  4 => Google_Service_Webmasters_UrlCrawlErrorCountsPerType {#307 ▶}
  5 => Google_Service_Webmasters_UrlCrawlErrorCountsPerType {#308 ▶}
  6 => Google_Service_Webmasters_UrlCrawlErrorCountsPerType {#309 ▶}
  7 => Google_Service_Webmasters_UrlCrawlErrorCountsPerType {#310 ▶}

I can get the category and platform but cannot work out how to access the count within #modelData>entries>0>count

  • 写回答

2条回答 默认 最新

  • doushi4864 2016-08-05 09:01
    关注

    I guest your respond is similar to Json, If that, your can try $responds_decode= json_decode("string json", true) Then it would be accessed the object like an array.

    评论

报告相同问题?