질문
여러 페이지의 결과를 자동으로 가져오려면 어떻게 해야 하나요?
답변
종종 여러 페이지의 사용자 결과를 가져와서 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 파일에 결과를 출력한 다음, 아래의 두 명령 중 하나를 실행하면 됩니다. 첫 번째는 고유 결과의 개수를, 두 번째는 실제 고유 값을 나타냅니다.
cat results.json | jq '.' | grep '"id"' > sort > uniq -c > uniq.txt
cat results.json | jq '.' | grep '"id"' > sort > uniq > uniq.txt
이러한 명령을 사용하려면 jq가 있어야 하며, 이는 https://stedolan.github.io/jq/download/에서 다운로드할 수 있습니다.
또한 이 정보는 새 시스템으로 가져오기 위해 데이터를 내보내는 데에도 유용할 수 있습니다.
번역 고지 사항: 본 문서는 콘텐츠에 대한 기본적인 이해를 제공하기 위해 자동 번역 소프트웨어를 사용하여 번역되었습니다. 정확한 번역을 제공하고자 합당한 노력을 기울였으나 Zendesk는 번역의 정확성을 보장하지 않습니다.
번역된 문서에 포함된 정보의 정확성과 관련하여 질문이 있으시면 문서의 공식 버전인 영문 버전을 참조하시기 바랍니다.
댓글 0개