<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Goolge Search Console APIs &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/goolge-search-console-apis/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Fri, 15 Jul 2022 05:42:22 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>Goolge Search Console APIs &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>PHP Client 操作 Google APIs (4) 使用 Google Search Console APIs</title>
		<link>https://noter.tw/9035/google-apis-4-google-search-console-apis/</link>
					<comments>https://noter.tw/9035/google-apis-4-google-search-console-apis/#comments</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Tue, 13 Jul 2021 07:30:16 +0000</pubDate>
				<category><![CDATA[其他]]></category>
		<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[技術相關]]></category>
		<category><![CDATA[Google Cloud Platform]]></category>
		<category><![CDATA[Goolge Search Console APIs]]></category>
		<category><![CDATA[開發]]></category>
		<category><![CDATA[教學]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[記下來]]></category>
		<category><![CDATA[Google APIs]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=9035</guid>

					<description><![CDATA[<p>這系列的最後一篇，原本以為已經寫完了，今天發現這篇躺在草稿裡 &#8230; 還不清楚怎麼回事的朋友可以看前面幾篇 PHP Client 操作 Google APIs (1) 開啟與測試 API 、 &#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/9035/google-apis-4-google-search-console-apis/" data-wpel-link="internal">PHP Client 操作 Google APIs (4) 使用 Google Search Console APIs</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>這系列的最後一篇，原本以為已經寫完了，今天發現這篇躺在草稿裡 &#8230; 還不清楚怎麼回事的朋友可以看前面幾篇 <a style="font-size: revert;" rel="noreferrer noopener" href="https://noter.tw/8940/google-apis-1/" target="_blank" data-wpel-link="internal">PHP Client 操作 Google APIs (1) 開啟與測試 API</a> 、 <a style="font-size: revert;" rel="noreferrer noopener" href="https://noter.tw/8944/google-apis-2/" target="_blank" data-wpel-link="internal">PHP Client 操作 Google APIs (2) 安裝 Google API Client for PHP</a> 及 <a href="https://noter.tw/8942/google-apis-3-service-account-credentials/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">PHP Client 操作 Google APIs (3) Google APIs Credentials 介紹</a>，這篇記錄如何使用 Google Search Console APIs。 </p>



<span id="more-9035"></span>



<p>我們在前幾篇裡面瞭解了 Google APIs、安裝 Client for PHP 以及申請了 Google APIs Credentials 裡的 Service Account 並下載了 JSON (一定要好好保存，遺失了只能刪除再產生一次，沒辦法再次下載喔)，開始使用！</p>



<h2 class="para wp-block-heading">Google APIs 大致流程</h2>



<p>使用 Google APIs 大致上的流程都差不多</p>



<ul class="my-li bg-darkblue wp-block-list"><li>載入 Google APIs Client for PHP (或其他語言版本)</li><li>加入 Service Account</li><li>建立並初始化物件</li><li>設定篩選條件</li><li>取得並解析結果</li></ul>



<p>只要把握這幾個原則，之後若要使用 Google APIs 就不會再霧沙沙了 (但 API 還是要查，每個 API 的 Scope 跟條件設定上會有很大的不同)</p>



<h2 class="para wp-block-heading">載入 Google APIs Client for PHP</h2>



<p>這個在前面  <a style="font-size: revert;" rel="noreferrer noopener" href="https://noter.tw/8944/google-apis-2/" target="_blank" data-wpel-link="internal">PHP Client 操作 Google APIs (2) 安裝 Google API Client for PHP</a>  的時候已經講過了，在程式碼裡面很簡單，開頭放上這行就可以了，為了更方便 debug，可以多設定兩行條件，只要使用上出現錯誤，就會直接在網頁上呈現</p>



<pre class="wp-block-preformatted withcode">// 顯示錯誤
ini_set('display_errors', '1');
// 所有錯誤都顯示
error_reporting(E_ALL);
// 載入 Google Client
require_once '../googleapiclient/vendor/autoload.php';</pre>



<h2 class="para wp-block-heading">加入 Service Account</h2>



<p>小蛙前面提到這次要做的事情是讓程式不經人工自動執行，而 Search Console 能使用的方式只有 OAuth 與 Service Account (忘記差別的這邊請 <a href="https://noter.tw/8942/google-apis-3-service-account-credentials/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">PHP Client 操作 Google APIs (3) Google APIs Credentials 介紹</a>)，要不需人工介入只有 Service Account 了，而要讓 Service Account 存取 Search Console 資料的話，就必須將該帳號加入 Search Console 裡面囉！</p>



<p>首先進入 Search Console 要使用的資源，左邊選單下面找到「設定」</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" src="https://lh3.googleusercontent.com/pw/AM-JKLXzIBu7ISSOmPScFy7qVA96IJdZN6hn_2sC8u8CAQo6ilW2TSsoE7VnZcq8sj1gRGhJqQ6wc_wjaC6MNwP3aY-rbsd89loU6C4h5Abb520EUYBS_0TD5NMys3eYYxweQuQwqvchEjMtFMJr9_65SCb-QA=w326-h243-no?authuser=2" alt="AM JKLXzIBu7ISSOmPScFy7qVA96IJdZN6hn 2sC8u8CAQo6ilW2TSsoE7VnZcq8sj1gRGhJqQ6wc wjaC6MNwP3aY rbsd89loU6C4h5Abb520EUYBS 0TD5NMys3eYYxweQuQwqvchEjMtFMJr9 65SCb QA=w326 h243 no?authuser=2 PHP Client 操作 Google APIs (4) 使用 Google Search Console APIs" title="PHP Client 操作 Google APIs (4) 使用 Google Search Console APIs"></figure></div>


<p>點擊「使用者與權限」</p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img fetchpriority="high" decoding="async" src="https://lh3.googleusercontent.com/pw/AM-JKLXZUyX8xf-BHak-20EBkiO2TFTuTXL_INR2QUGFYk7pHLNsIPtwfApwqJ1xcShRkvqj2low9Iuh4ksHN79DSkUqUjylMiyEd1MMK0DO5P2U1X-ZfMa9lKLLv8EyrMp1-j3pXSYrSGxt8gh5MKlyxeN7ww=w817-h394-no?authuser=2" alt="AM JKLXZUyX8xf BHak 20EBkiO2TFTuTXL INR2QUGFYk7pHLNsIPtwfApwqJ1xcShRkvqj2low9Iuh4ksHN79DSkUqUjylMiyEd1MMK0DO5P2U1X ZfMa9lKLLv8EyrMp1 j3pXSYrSGxt8gh5MKlyxeN7ww=w817 h394 no?authuser=2 PHP Client 操作 Google APIs (4) 使用 Google Search Console APIs" width="613" height="296" title="PHP Client 操作 Google APIs (4) 使用 Google Search Console APIs"></figure></div>


<p>點擊「新增使用者」並把從 GCP Credentials 那邊得到的 Service Account 貼上，權限選擇「完整」</p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img decoding="async" src="https://lh3.googleusercontent.com/pw/AM-JKLXEF6Sy2MrLHsvzAKbOuqRzWdPB-kwXigP9LAgaaBdiHnlavX2vwEvWRLMW6NS4nJi_mkQlhcD0ldCVhN4KoVx_D_azli_64i9YbL1nDF7HEzeBvMSG5mS0gdHUv_YeADHYPcEgZqaWH6uKZFM4gwAj5A=w746-h385-no?authuser=2" alt="AM JKLXEF6Sy2MrLHsvzAKbOuqRzWdPB kwXigP9LAgaaBdiHnlavX2vwEvWRLMW6NS4nJi mkQlhcD0ldCVhN4KoVx D azli 64i9YbL1nDF7HEzeBvMSG5mS0gdHUv YeADHYPcEgZqaWH6uKZFM4gwAj5A=w746 h385 no?authuser=2 PHP Client 操作 Google APIs (4) 使用 Google Search Console APIs" width="560" height="289" title="PHP Client 操作 Google APIs (4) 使用 Google Search Console APIs"></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" src="https://lh3.googleusercontent.com/pw/AM-JKLVdteLUJJMU-tjYJzk3UQDQIhDk0n7Q36DgbhQKKbu3n57YYr2H1aPYTP0krgU5rBXxSGz8_z40ZmiJEOCaGJKCK-XhvCFhYdVza6Zs3z1Y2T5P7DAshqm-rDHu4jGLKaBkisrRs7fcjXUvlPo5Lh56mw=w1004-h385-no?authuser=2" alt="AM JKLVdteLUJJMU tjYJzk3UQDQIhDk0n7Q36DgbhQKKbu3n57YYr2H1aPYTP0krgU5rBXxSGz8 z40ZmiJEOCaGJKCK XhvCFhYdVza6Zs3z1Y2T5P7DAshqm rDHu4jGLKaBkisrRs7fcjXUvlPo5Lh56mw=w1004 h385 no?authuser=2 PHP Client 操作 Google APIs (4) 使用 Google Search Console APIs" title="PHP Client 操作 Google APIs (4) 使用 Google Search Console APIs"></figure></div>


<h2 class="para wp-block-heading">建立並初始化物件</h2>



<p>回到 PHP 的部份加入金鑰及設定 Scope</p>



<pre class="wp-block-preformatted withcode">// 設定金鑰
putenv('GOOGLE_APPLICATION_CREDENTIALS=存放金鑰的路徑/server_secrets.json');
session_start();

// 建立基本 Google Client 
$client = new Google_Client();
// 載入預設金鑰位置
$client-&gt;useApplicationDefaultCredentials();
// 設定 Scope
$client-&gt;addScope([Google_Service_Webmasters::WEBMASTERS_READONLY, Google_Service_Webmasters::WEBMASTERS]);
$client-&gt;setAccessType('offline');
// 建立基本 Google Service Webmasters 物件
$master = new Google_Service_Webmasters($client);</pre>



<h2 class="para wp-block-heading">設定篩選條件</h2>



<p>建立完要用的 Google Service Webmasters 物件之後，對！以前 Google Search Console 叫做 Google Webmaster，雖然我們說要操作 Search Console 但建立的物件跟設定的 Scope 可以看出都還是 Webmasters。</p>



<pre class="wp-block-preformatted withcode">$search = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$search-&gt;setStartDate( '2021-07-06' );
$search-&gt;setEndDate( '2021-08-06' );
$search-&gt;setDimensions( ['QUERY'] );
$search-&gt;setAggregationType( 'BY_PROPERTY' );
$search-&gt;setRowLimit(10);</pre>



<p>至於有哪些條件或是要怎麼操作取得自己要的內容，就自己到 <a href="https://developers.google.com/webmaster-tools/search-console-api-original/v3/searchanalytics/query" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">API Explorer<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 測試，或到 <a href="https://noter.tw/8940/google-apis-1/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">PHP Client 操作 Google APIs (1) 開啟與測試 API</a> 複習吧！</p>



<h2 class="para wp-block-heading">取得並解析結果</h2>



<p>最後把他們全部串起來～丟回的結果可以先用 print_r 印出來看看，或是直接到 <a href="https://developers.google.com/webmaster-tools/search-console-api-original/v3/searchanalytics/query" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">這邊<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 看回傳的結果 ~</p>



<pre class="wp-block-preformatted withcode">$data = $master-&gt;searchanalytics-&gt;query('https://noter.tw/', $search);

$result = [];
$rows = $data-&gt;rows;
foreach ($rows as $row){
    // 剖析資料
}

</pre>



<p>到這邊基本上就大功告成了，上面的功能是取得網站上特定區間的關鍵字資訊 ~ 需要其他資料的話，釣竿已經在手了，就要靠自己囉！Good Luck ~ </p>



<p id="block-c58a739f-b4e1-42ae-8530-5e20da347253"><strong>Google 系列文章：</strong></p>



<ul class="wp-block-list" id="block-2b1f3c8f-95c7-4eff-8105-53dea9d75e5e"><li><a href="https://noter.tw/8940/google-apis-1-api-explorer/" data-wpel-link="internal">PHP Client 操作 Google APIs (1) 開啟與測試 API</a></li><li><a href="https://noter.tw/8944/google-apis-2-google-api-client-for-php/" data-wpel-link="internal">PHP Client 操作 Google APIs (2) 安裝 Google API Client for PHP</a></li><li><a href="https://noter.tw/8942/google-apis-3-service-account-credentials/" data-wpel-link="internal">PHP Client 操作 Google APIs (3) Google APIs Credentials 介紹</a></li><li><a href="https://noter.tw/9035/google-apis-4-google-search-console-apis/" data-wpel-link="internal">PHP Client 操作 Google APIs (4) 使用 Google Search Console APIs</a></li><li><a href="https://noter.tw/8604/google-formranger/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">Google 表單應用：設定選項次數上限</a></li><li><a href="https://noter.tw/9049/copy-a-google-form/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">複製 Google 表單 (Copy A Google Form)</a></li><li><a href="https://noter.tw/8358/google-adsense-tax-w8ben/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">Google Adsense: 向 Google 提交稅務資料 | 填寫 W-8BEN 稅務表單</a></li><li><a href="https://noter.tw/4946/google-%e9%9b%b2%e7%ab%af%e5%a4%96%e5%b8%b6%e6%9c%8d%e5%8b%99-takeout%ef%bc%9a%e8%bc%95%e9%ac%86%e6%89%93%e5%8c%85%e9%9b%b2%e7%ab%af%e7%a1%ac%e7%a2%9f%e3%80%81%e7%9b%b8%e7%b0%bf-%e7%ad%89/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">Google 雲端外帶服務 (Takeout)：輕鬆打包雲端硬碟、相簿 … 等資料</a></li><li><a href="https://noter.tw/4394/google-%e7%9b%b8%e7%b0%bf%e5%8f%96%e5%be%97%e7%9c%9f%e5%af%a6%e5%9c%96%e7%89%87%e4%bd%8d%e7%bd%ae%ef%bc%88%e6%8f%92%e5%85%a5%e5%9c%96%e7%89%87%e5%88%b0%e6%96%87%e7%ab%a0%ef%bc%89/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">Google 相簿取得真實圖片位置（插入圖片到文章）</a></li><li><a href="https://noter.tw/4267/gdirve-%e8%ae%93%e4%bd%a0%e5%9c%a8-linux-%e6%96%87%e5%ad%97%e4%bb%8b%e9%9d%a2%e4%b9%9f%e8%83%bd%e5%a5%bd%e5%a5%bd%e4%bd%bf%e7%94%a8-google-drive/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">gdirve 讓你在 Linux 文字介面也能好好使用 Google Drive</a></li><li><a href="https://noter.tw/4302/microsoft-office-word-%e7%84%a1%e6%b3%95%e9%96%8b%e5%95%9f-google-drive-%e8%b6%85%e9%80%a3%e7%b5%90/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">Microsoft Office Word 無法開啟 Google Drive 超連結</a></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/9035/google-apis-4-google-search-console-apis/" data-wpel-link="internal">PHP Client 操作 Google APIs (4) 使用 Google Search Console APIs</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/9035/google-apis-4-google-search-console-apis/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
