質問
複数ページの結果を自動的に取得するにはどうすればよいですか?
回答
ユーザーの結果を数ページ取得し、JSONファイルに配置すると便利な場合があります。このためにPHPスクリプトを作成しました。これを添付し、また以下に貼り付けています。
<?php
$resource = ''; // put the name of the endpoint here, for example organizations, tickets
$start_page = 1; // first page you want to load
$end_page = 10; // last page you want to load (it could take a while if too big)
$subdomain = ''; // your zendesk subdomain
$userpwd = ''; // your zendesk username and password, or username/token and api token. - {username}/token:{token} or {username}:{password}
function makerequest($url, $userpwd) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
$result = curl_exec($ch);
if (curl_errno($ch)) $result = curl_error($ch);
curl_close($ch);
return $result;
}
function getpages($resource, $start_page, $end_page, $userpwd, $subdomain){
$url = isset($url) ? $url : "https://$subdomain.zendesk.com/api/v2/$resource.json";
$i = $start_page;
$array = Array();
while (1) {
if ($i > $end_page) {
$file = fopen("results.json", "w") or exit("Unable to open file!");
fwrite($file, json_encode(array($resource => $array)));
fclose($file);
break;
}
$results = json_decode(makerequest($url, $userpwd));
$array = array_merge($array, $results->$resource);
$url = $results->next_page;
$i++;
}
}
getpages($resource, $start_page, $end_page, $userpwd, $subdomain);
?>
このスクリプトをダウンロードするか、コピーして「paginate.php」というファイルに貼り付けます。 スクリプトのコメントで説明したように、最初に、変数に「$resource」、「$start_page」、「$end_page」、「$subdomain」、および「$userpwd」を入力します。
次に、ファイルをダウンロードしたフォルダに移動し、端末からコマンド「php paginate.php」を実行して、「results.json」という新しいファイルを作成し、結果をファイル内に格納します。ファイルは上書きするように設計されており、追加することはできません。
結果を受け取ったら、必要に応じて処理することができます。この情報は、配列内の一意であったエントリと複製されたエントリを見つけるのに役立ちます。これを実行するには、結果を「results.json」ファイルに出力し、以下の2つのコマンドのいずれかを実行します。最初のコマンドは、一意な結果の件数を表示し、2つ目のコマンドは実際の一意な値を表示します。
cat results.json | jq '.' | grep '"id"' > sort > uniq -c > uniq.txt
cat results.json | jq '.' | grep '"id"' > sort > uniq > uniq.txt
これらのコマンドを使用するには、https://stedolan.github.io/jq/download/からjqをダウンロードする必要があります。
さらに、この情報は、インポートするデータを新しいシステムにエクスポートするのに役立ちます。
翻訳に関する免責事項:この記事は、お客様の利便性のために自動翻訳ソフトウェアによって翻訳されたものです。Zendeskでは、翻訳の正確さを期すために相応の努力を払っておりますが、翻訳の正確性については保証いたしません。
翻訳された記事の内容の正確性に関して疑問が生じた場合は、正式版である英語の記事を参照してください。
0件のコメント