亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

組合作為多個塊發送的大型 JSON

組合作為多個塊發送的大型 JSON

PHP
慕村225694 2021-12-03 16:20:42
我正在使用 cURL 和 PHP 來訪問 Salesforce API,并且其中大部分都在工作。當您請求大量記錄時,Salesforce 會將數據分成 2000 塊。基本上,我有三個不同的大 JSON 塊和記錄數組,我想知道將它們組合成一個大對象的最佳方法是什么。當有更多數據時,對象看起來像這樣。它為您提供了一個 nextRecordsUrl,您可以點擊它來獲取下一個塊:Array(    [totalSize] => 5000    [done] =>     [nextRecordsUrl] => /services/data/v20.0/query/somelongurlstring    [records] => Array        (            [0] => Array                (                    [attributes] => Array                        (                            [type] => Custom_Type__c                            [url] => sobjects/Custom_Type__c/thetypeid                        )                    [Requested_Prop__c] => someid                )            [1] => Array                ( [...]我的 curl 代碼似乎正在運行 - 基本上是這樣的:curl_setopt($ch2, CURLOPT_URL, "https://my.instance.salesforce.com/services/data/v20.0/query/?q=" . urlencode($_GET["q"]));curl_setopt($ch2, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer $token"));curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "GET");curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, 1);$isDone = false;while(!$isDone) {    $qresponse = curl_exec($ch2);    echo "<pre>";    $theJson = json_decode($qresponse, true);    echo print_r($theJson);    echo "</pre>";    $isDone = $theJson["done"] == true;    if(!$isDone)        curl_setopt($ch2, CURLOPT_URL, "https://my.instance.salesforce.com" . $theJson["nextRecordsUrl"]);}curl_close($ch2);問題是輸出實際上是多個json對象。我認為最簡單的方法可能是簡單地遍歷記錄并將它們附加到我在 PHP 中創建的某個 json 對象,但我擔心的只是如果這些獲取請求變得非常大可能需要的內存量。有沒有“正確的方法”來做到這一點?
查看完整描述

1 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

我最終只是自己附加數據:


$isDone = false;

$finalJson = array();

while(!$isDone) {

    $qresponse = curl_exec($ch2);

    $theJson = json_decode($qresponse, true);

    $isDone = $theJson["done"] == true;

    foreach($theJson["records"] as $record) {  //Only return records

        $finalJson[] = $record;

    }

    if(!$isDone)

        curl_setopt($ch2, CURLOPT_URL, "https://my.instance.salesforce.com" . $theJson["nextRecordsUrl"]);

}

echo "<pre>" . json_encode($finalJson, JSON_PRETTY_PRINT) . "</pre>";


查看完整回答
反對 回復 2021-12-03
  • 1 回答
  • 0 關注
  • 146 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號