2

我是 REST 的新手,并试图SELECT ID from CO.A_Country where Name="xxx";使用 ROQL 从查询中获取 ID。我将 REST 与 PHP 一起使用:

<?php
/**
 *  Example API call
 *  GET profile information
 */

// the ID of the profile

$profileID = 2;

// the token

$token = 'your token here';

// set up the curl resource

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
            "https://User:Pass@test.help.com/services/rest/connect/v1.3/queryResults
 /?query=select%20ID%20from%20CO.A_Country%20where%20CO.A_Country.A_Country=%27xxx%27");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$output = curl_exec($ch);

echo($output). PHP_EOL;

curl_close($ch);

?>

我得到以下结果:

HTTP/1.1 200 OK Date: Thu, 04 Aug 2016 14:58:02 GMT Server: Apache Content-Language: en-US RNT-Time: D=58455 t=1470322682582920 RNT-Machine: 128.64 Transfer-Encoding: chunked Content-Type: application/json { "items": [ { "tableName": "CO.Affected_Country", "count": 1, "columnNames": [ "id" ], "rows": [ [ "12" ] ] } ], "links": [ { "rel": "self", "href": "https://test.help.com/services/rest/connect/v1.3/queryResults?query=select%20ID%20from%20CO.A_Country%20where%20CO.A_Country.Affected_Country=%27USA%27" }, { "rel": "canonical", "href": "https://test.help.com/services/rest/connect/v1.3/queryResults" }, { "rel": "describedby", "href": "https://test.help.com/services/rest/connect/v1.3/metadata-catalog/queryResults", "mediaType": "application/schema+json" } ] }

如何仅获取所需的 ID 以在 $output 中?

4

2 回答 2

1

您不能从 OSvC REST API 的 ROQL 查询中获取 ID;特别是因为您的查询可能会返回多个结果。此其他数据将始终作为结果的一部分返回(以及其他相关数据,具体取决于您使用的端点)。您需要解析这些结果以从查询中获取 ID。

不要将标头作为响应正文的一部分返回:

curl_setopt($ch, CURLOPT_HEADER, 0);

然后按照@Manish Shukla 的建议,使用 json_decode 将 ROQL 结果转换为 PHP 对象。

$results = json_decode($output);

然后,您的 ROQL 结果是可解析的。行作为可以循环的数组返回。由于您的查询没有限制语句,因此您应该添加该语句或考虑返回的多个值。

if(!is_null($results['items'][0]['rows'])){ foreach($results['items'][0]['rows'] as $row){ echo $row; //Will output the ID from your select statement. Change this to do something with the ID as needed. } }

于 2016-08-05T13:22:21.737 回答
0

基本上 curl 将完整的数据作为 json 返回给您。首先将此数据转换为 php,然后您可以遍历该数组以找到确切的值。你可以使用 json_decode 函数。有关功能的更多详细信息,请参阅此。 http://php.net/manual/en/function.json-decode.php

于 2016-08-04T15:22:54.993 回答