首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >现在使用从REST GET请求中获取ID

现在使用从REST GET请求中获取ID
EN

Stack Overflow用户
提问于 2016-08-04 15:05:24
回答 2查看 1.1K关注 0票数 2

我是REST的新手,只想使用ROQL从查询SELECT ID from CO.A_Country where Name="xxx";获取ID。我在PHP中使用REST:

代码语言:javascript
复制
<?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" } ] }

如何在$output中只获得所需的ID?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-05 13:22:21

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

不要将标题作为响应主体的一部分返回:

curl_setopt($ch, CURLOPT_HEADER, 0);

然后使用json_decode将ROQL结果转换为PHP对象,就像@Manish建议的那样。

$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. } }

票数 1
EN

Stack Overflow用户

发布于 2016-08-04 15:22:54

基本上curl是以json的形式返回完整的数据。首先,将这些数据转换为php,然后遍历该数组以找到确切的值。您可以使用json_decode函数。有关功能的更多细节,请参考以下内容。http://php.net/manual/en/function.json-decode.php

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38771043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档