Can someone sanity-check this for me please? Its pulls records from a CRM via API and filling them into a list box. I copied it from a PHP script that I had an tried to do the same thing in Xojo, but its not working as expected.
The PHP Code works fine.
//API credentials
Var apiUsername As String = "username here "
Var apiPassword As String = "Password here"
Var integrationCode As String = "Code here"
// Autotask API endpoint
Var apiEndpoint As String = "https://webservices16.autotask.net/atservicesrest/V1.0"
// Autotask API URL to retrieve list of companies
Var url As String = apiEndpoint + "/Companies/query?search={\"filter\":[{\"op\":\"eq\",\"field\":\"CompanyType\",\"value\":\"1\"}]}"
Var response As String =URLConnection1.Send("GET", url)
// Parse JSON response
Var json As New JSONItem(response)
//Set HTTP Request and get Response
URLConnection1.SetRequestContent("Content-type", "application/json')
URLConnection1.RequestHeader("ApiIntegrationCode", integrationCode)
URLConnection1.RequestHeader("UserName", apiUsername)
URLConnection1.RequestHeader("Secret", apiPassword)
// Extract company data from JSON response
Var companies() As Dictionary
For Each account As JSONItem In json.Value("items")
Var company As New Dictionary
company.Value("id") = account.Value("id").ToInteger
company.Value("name") = account.Value("companyName").ToString
company.Value("phone") = account.Value("phone").ToString
company.Value("website") = account.Value("webAddress").ToString
company.Value("city") = account.Value("city").ToString
company.Value("state") = account.Value("state").ToString
company.Value("country") = account.Value("countryID").ToString
companies.Add(company)
Next
// Display company data
For Each company As Dictionary In companies
ListBox1.AddRow("ID: " + company.Value("id").ToString)
ListBox1.AddRow("Name: " + company.Value("name").ToString)
ListBox1.AddRow("Phone: " + company.Value("phone").ToString)
ListBox1.AddRow("Website: " + company.Value("website").ToString)
ListBox1.AddRow("City: " + company.Value("city").ToString)
ListBox1.AddRow("State: " + company.Value("state").ToString)
ListBox1.AddRowt("Country: " + company.Value("country").ToString)
Next
I am also getting a Syntax error on this line so not sure what it should be
Var url As String = apiEndpoint + "/Companies/query?search={\"filter\":[{\"op\":\"eq\",\"field\":\"CompanyType\",\"value\":\"1\"}]}"
Here is the original PHP Code (which works perfectly fine)
<?php
// Autotask API credentials
$apiUsername = 'Username';
$apiPassword = 'Password';
$integrationCode = 'Code';
// Autotask API endpoint
$apiEndpoint = 'https://webservices16.autotask.net/atservicesrest/V1.0';
// Autotask API URL to retrieve list of companies
$url = $apiEndpoint . '/Companies/query?search={"filter":[{"op":"eq","field":"CompanyType","value":"1"}]}';
// Set HTTP headers
$headers = array(
'Content-Type: application/json',
'ApiIntegrationCode: ' . $integrationCode,
'UserName:' . $apiUsername,
'Secret: ' . $apiPassword
);
// Set HTTP authentication credentials
$auth = array(
$apiUsername,
$apiPassword
);
// Set HTTP context options
$options = array(
'http' => array(
'method' => 'GET',
'header' => implode("\r\n", $headers),
'auth' => implode(":", $auth),
'ignore_errors' => true
)
);
// Create HTTP context
$context = stream_context_create($options);
// Send HTTP request and get response
$response = file_get_contents($url, false, $context);
// Parse JSON response
$json = json_decode($response, true);
// Check if JSON parsing succeeded
if (json_last_error() !== JSON_ERROR_NONE) {
die('Error: Unable to parse JSON response');
}
// Extract company data from JSON response
$companies = array();
foreach ($json["items"] as $account) {
$company = array(
'id' => (int) $account['id'],
'name' => (string) $account['companyName'],
'phone' => (string) $account['phone'],
'website' => (string) $account['webAddress'],
'city' => (string) $account['city'],
'state' => (string) $account['state'],
'country' => (string) $account['countryID']
);
array_push($companies, $company);
}
// Display company data in a grid
echo '<table>';
echo '<tr><th>ID</th><th>Name</th><th>Phone</th><th>Website</th><th>City</th><th>State</th><th>Country</th></tr>';
foreach ($companies as $company) {
echo '<tr>';
echo '<td>' . $company['id'] . '</td>';
echo '<td>' . $company['name'] . '</td>';
echo '<td>' . $company['phone'] . '</td>';
echo '<td>' . $company['website'] . '</td>';
echo '<td>' . $company['city'] . '</td>';
echo '<td>' . $company['state'] . '</td>';
echo '<td>' . $company['country'] . '</td>';
echo '</tr>';
}
echo '</table>';