2016-08-04 10 views
2

私はRESTで新しく、ROQLを使用してクエリSELECT ID from CO.A_Country where Name="xxx";からIDのみを取得しようとしています。私はPHPでRESTを使用しています:PHPでREST GETリクエストからIDを取得する - Oracle Right Now

<?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:[email protected]/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); 

>

私は次のような結果になっています:?$出力にあることが唯一の希望IDを取得する方法

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" } ] }

を?

答えて

1

あなただけ OSvC REST APIからROQLクエリからIDを取得できませんことができます。特にクエリが複数の結果を返す可能性があるためです。この他のデータは、結果(および使用するエンドポイントに応じてその他の関連データ)の一部として常に返されます。クエリからIDを取得するには、これらの結果を解析する必要があります。

はレスポンスボディの一部としてヘッダを返さない:

curl_setopt($ch, CURLOPT_HEADER, 0);

そして@Manishシュクラが示唆するように、PHPオブジェクトにROQL結果を変換するjson_decodeを使用します。

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

+0

これは私が探しているものです。よろしく – Walid

0

基本的にはcurlが完全なデータをjsonとして返しています。まず、このデータをPHPに変換して、その配列をトラバースして正確な値を見つけることができます。 json_decode関数を使用できます。機能の詳細については、こちらを参照してください。 http://php.net/manual/en/function.json-decode.php

関連する問題