如何使用php在内容丰富的登录页中循环不同的部分

如何使用php在内容丰富的登录页中循环不同的部分,php,contentful,contentful-api,Php,Contentful,Contentful Api,使用PHPSDK,我希望获得登录页的所有内容,这将包括具有不同内容类型/组件的部分,每个条目具有不同的字段 例如 第一节英雄 第2节-第条 第3节-图像 Q.如何循环浏览并获取链接的部分内容。 如果条目包含指向资产或其他资产的链接 输入时,SDK将自动加载它 我正在努力理解这个问题。我如何准确地分解这些值 <?php $query = new \Contentful\Delivery\Query(); $query->setContentType('page_landing') -&

使用PHPSDK,我希望获得登录页的所有内容,这将包括具有不同内容类型/组件的部分,每个条目具有不同的字段

例如

第一节英雄

第2节-第条

第3节-图像

Q.如何循环浏览并获取链接的部分内容。

如果条目包含指向资产或其他资产的链接 输入时,SDK将自动加载它

我正在努力理解这个问题。我如何准确地分解这些值

<?php
$query = new \Contentful\Delivery\Query();
$query->setContentType('page_landing')
->where("fields.urlHandle[match]", $slug);

$products = $client->getEntries($query);

foreach ($products as $product) {

echo $product['name'];

// Example of what I am trying to achieve
    if($product['sections']['id']=='hero'){
      $title= $product['sections']['hero']['title'];
    } 
    if($product['sections']['id']=='article'){
      $text = $product['sections']['article']['text'];
    } 
}
?>

我想知道是否有更好的方法,文档中没有很多示例。下面是我结束的地方,但我觉得必须有一个更简单的方法

<?php
require_once 'vendor/autoload.php';
require_once 'vendor/credentials.php';

// if we need to use rich text 
$renderer = new \Contentful\RichText\Renderer();

// Get the section entry id's
foreach ($products as $product) {
    $dataJson     = json_encode($product);
    $revertJson[] = json_decode($dataJson, true);
    foreach ($revertJson as $Json) {
        foreach ($Json['fields']['sections'] as $entries) {
            $entryID[] = $entries['sys']['id'];
        }
    }
}

// Loop out the sections
foreach ($entryID as $entry) {
    // Get individual linked entries
    $entry = $client->getEntry($entry);
    $type  = $entry->getSystemProperties()->getcontentType();
    $json  = json_encode($type);
    $comp  = json_decode($json, true);
    if ($comp['sys']['id'] == 'Hero') {
        // field
        echo $entry['urlHandle'];
        // rich text
        echo $renderer->render($entry['text']);
        // asset
        $asset = $client->getAsset($entry['imageId']);
        echo $asset->getFile()->getUrl();
    } elseif ($comp['sys']['id'] == 'Landmark') {
        echo $entry['title'];
    }
}