簡體   English   中英

你怎么能從 PHP 中的 API 得到 stream 結果

[英]How can you stream results from an API in PHP

PHP 中是否有一種方法,可能使用外部庫,以響應 API 數據的 stream 結果?

例如,我有以下代碼來獲取數據:

$resultsAPI = "https://www.example.com/api/results.json? 
app_id=$app_id&token=$token&page=1&limit=10";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $resultsAPI);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json;api_version=2' ));
$resp = curl_exec($curl);
curl_close($curl);
$results = json_decode($resp, true)['results'];

foreach ($results as $key=>$resultImage) {
    $resultImage= "$resultImage[images]?app_id=$app_id&token=$token";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $resultImage);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $resp = curl_exec($curl);
    curl_close($curl);
    $image = json_decode($resp, true);
    $results[$key]['image1'] = $image['image1'];
}

echo '<div class="card"><ul>';
foreach ($results as $result) {
echo '<li>';
echo '<span><p>'.$result['title'].'</p></span>';
echo '<span><p>'.$result['description'].'</p></span>';
echo '<span><img src="'.$result['image1'].'"></span>';
echo '</li>';
}
echo '</ul></div>';

加載所有數據可能需要一些時間,因為它將遍歷一些大文件。 當它有第一個數據時,是否可以開始流式傳輸結果?

在下圖中,它顯示了我要解釋的內容。 數據正在被一一加載到骨架中:

流加載結果

對此的任何想法都會非常有幫助,或者如果有可能的話。

我認為你走錯了方向。

HTML 在加載所有 html 后開始渲染。 因此,使用 stream 獲取 html 不適合您。

在演示中,它只加載了一個簡單的 html 頁面。 然后使用 ajax 之類的內容加載頁面的其他部分。 每次加載一個零件然后渲染它。

為什么不合並 foreach 循環? 我沒有對此進行測試,但項目應該非常迭代地呼應。

echo '<div class="card"><ul>';
foreach ($results as $key=>$result) {
    $resultImage= "$result[images]?app_id=$app_id&token=$token";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $resultImage);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $resp = curl_exec($curl);
    curl_close($curl);
    $image = json_decode($resp, true);
    $results[$key]['image1'] = $image['image1'];

   echo '<li>';
   echo '<span><p>'.$result['title'].'</p></span>';
   echo '<span><p>'.$result['description'].'</p></span>';

   if(isset($image['image1'])){
       echo '<span><img src="'.$result['image1'].'"></span>';
   }
   echo '</li>';
}
echo '</ul></div>';

我希望這會有所幫助

As Kris Roofe said you may need ajax or axios call to output your data with some effects and animations exactly like the image that you attached in your question, your application need to be rendered first with all of it's HTML tags and assets like CSS and Javascript files then you can output some data and show it with animations by using ajax or axios and ofcurse you have more control over your data streaming in this case by using ajax or axios in client-side but you can also do it in your server-side但通常我更喜歡在客戶端做這些事情。

順便說一句,如果您堅持以這種方式執行此操作,您可以使用flush()ob_flush()在 while 循環結束之前立即 output 您的數據。 php 官方文檔鏈接中已經有人提到了這一點。 您可以查看並閱讀有關output 緩沖和這些方法的完整文檔。

您應該將兩個 foreach 循環合並在一起,然后將這兩個方法添加到循環的末尾,以便在每次循環迭代后立即將您的數據變為 output。

foreach($results as $key=>$resultImage){
    //fetch images data such as title, description, and image itself in this loop
    // and aslo echo your html tags in here. 
    // echo '<li>';
    // echo '<span><p>'.$result['title'].'</p></span>';
    // echo '<span><p>'.$result['description'].'</p></span>';
    // echo '<span><img src="'.$result['image1'].'"></span>';
    // echo '</li>';

}

我在你的 for 循環中寫了一些注釋來告訴你應該你的循環合並在一起,因為你試圖在你的第一個循環中初始化$results變量,然后在完成那個循環之后你正在迭代$results變量以顯示 output 數據. 所以你不能 output 數據立即在這里有兩個循環,因為你的第二個循環取決於第一個循環,它不會開始迭代,直到第一個循環完成 檢查我為演示這兩種方法的用法而編寫的這段小代碼:

$curl = curl_init();
for($i=0;$i<5;$i++){
    curl_setopt($curl, CURLOPT_URL, 'example.com');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($curl);
    $output = json_decode($output);
    foreach($output as $key=>$value){
        echo 'key: '.$key;
        echo '$value: '.$value;
        ob_flush();
        flush();
    }
}

強烈建議您閱讀有關output 緩沖的信息,以便在您的項目中正確實現它。

我希望這可以幫助你。

HTML 在其中一些到達時開始渲染。 它不必是完整的源代碼。 在 PHP 中,您可以通過ob_flush()flush()調用“發送您已經回顯的內容”。 這樣它會立即顯示在瀏覽器中。 這可以與 JSON stream 使用例如halaxa/json-machine解析配對,因此結果可能如下所示:

<?php
echo '<div class="card"><ul>';
foreach (JsonMachine::fromStream($jsonStreamResource) as $result) {
    echo '<li>';
    echo '<span><p>'.$result['title'].'</p></span>';
    echo '<span><p>'.$result['description'].'</p></span>';
    echo '<span><img src="'.$result['image1'].'"></span>';
    echo '</li>';
    ob_flush();
    flush();
}
echo '</ul></div>';

在首次加載 html 時獲取有限的記錄。 一旦頁面完全加載,然后調用 ajax function 它將獲取下一頁行。 絕對是經過測試的方法。

    <script>
    var items = [{item1}, {item2}];
    $(document).ready(function() {
    $.each(items, function(index, item) {
    $('.card').append('<li>'+ '<span><p>'+item['title']+'</p></span>' + 
    '</li>');
     });
     });`enter code here`
    </script>

或者,在 html 完全呈現后,您可以調用 ajax function 獲取首頁記錄。

如何從帶有 ajax 的文件中加載 JSON object?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM