How to grab individual fields from an API call?
Posted: Mon Nov 20, 2017 7:25 am
I'm new to API call and retrieving individual fields. This is an example of the API call page.
At this point, I'm not updating the article. Once all the articles are pulled, how do I go through each article and grab individual fields and store it in my MySQL database? For example, the fields are ID, title, name, summary, status, url_has etc..
Here's what the data returned looks like.
Thank you.
Code: Select all
public function updateArticleHtml()
{
$agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
$projectID = "{{123456789021}}";
$endPoint = "https://app.knowledgeowl.com/api/head/article?project_id={$projectID}&status=published";
$postparams = array();
$postparams['_authbykey'] = "{{123456789132}}";
$postparams['_fields'][] = 'current_version'; //if not specified will return all fields
$postparams = http_build_query($postparams);
//pull back the article html and update
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $endPoint . '?' . $postparams);
curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if(!empty($error))
{
//handle error
}
$json = json_decode($buffer, true);
if(!$json['valid'])
{
//handle error
}
else
{
$currentHTML = $json['data']['current_version']['en']['text'];
//update text
$updatedHTML = str_replace('some string', 'some new string', $currentHTML);
}
//push the updated html back into the article
$postparams = array();
$postparams['_authbykey'] = "{{API KEY}}";
$postparams['current_version']['en']['text'] = $updatedHTML;
$postparams['_method'] = 'PUT';
$postparams = http_build_query($postparams);
//pull back the article html and update
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $endPoint . '?' . $postparams);
curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postparams);
$buffer = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if(!empty($error))
{
//handle error
}
$json = json_decode($buffer, true);
if(!$json['valid'])
{
//handle error
}
else
{
//article updated successfully
}
}Here's what the data returned looks like.
Code: Select all
{
valid: true,
page_stats:
{
total_records: 2603,
total_pages: 6,
next_page: 2
},
data: [
{
id: "572ba7f3321342521cf2c72a",
type: "article",
summary: "",
project_id: "55c4ffd131254c527e294fe6",
name: "Student Test #1",
searchTitle:
{
en: "Student Test 1"
},
toc_title: "",
internal_title: null,
languages: null,
current_version:
{
en:
{
⁃ title: "Student Test #1",
⁃ text: "<p>blah blah bleee blaah</p> "
}
},
visibility: "public",
view_count: null,
category: "",
application_screens:
[
""
],
current_version_id: "572ba7fd99876c0f4dc15aaf",
current_version_number: 1,
date_created: "05/05/2016 3:07 pm CDT",
date_modified: "05/05/2016 3:09 pm CDT",
date_published: null,
date_deleted: null,
modified_author: "55c5006132131cc87f294f75",
status: "deleted",
index: 0,
category_view: "",
prevent_searching: "",
hide_from_toc: "",
remove_pdf: "",
remove_feedback: "",
remove_comments: "",
url_hash: "student-test-1",
callout: "none",
callout_expire: 1463083766,
callout_video: "",
auto_save:
{
time: 1462493400,
authorID: "55c50062345641cc87f294f75",
content: "<p>blah blah bleee blaah</p> "
},
reader_roles: "",
inherited_roles: null,
pdf: "/help/pdfexport/id/572ba7f332431c521cf2c72a",
related_articles:
[
""
],
content_article: null,
article_link: null,
redirect_link: null,
author: "55c5006132131cc87f294f75",
user_teams: "",
tags: "",
search_phrases: "",
ready_versions: null,
parents:
[
"572ba77c332541cda1bf2c72d"
],
meta_data: null,
meta_description: null,
meta_page_title: null,
template_article: null,
topic_article: null
},