Reviews.io 产品 API 中的 Foreach 循环

问题描述 投票:0回答:2

我对 API 世界还很陌生,目前正在尝试显示我的reviews.io 帐户上对特定产品的所有评论。我尝试制作以下脚本,现在似乎可以工作,但是,我希望在 for-each 循环中很好地对结果进行排序。

这是我当前的脚本:

    <?php
        $header_data = array(
            'store' => 'MYSHOP',
            'apikey' => 'MYKEY',
            'method'  => 'GET'
             );

        $ch = curl_init();
        $curlOpts = array(
            CURLOPT_URL => 'https://api.reviews.co.uk/product/review?store=MYSHOP&sku=TSCB20&apikey=MYKEY',
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_HEADER => false
        );  
        curl_setopt_array($ch, $curlOpts);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));   
        $rv = curl_exec($ch);
        $data = json_decode($rv,true);

        curl_close($ch);

    ?>

这会以原始代码或 JSON 形式输出所有评论,但我相信,我希望将它们很好地显示在每个项目的 div 中。

输出如下(已格式化):

    {
      "store": {
        "name": "",
        "logo": ""
      },
      "stats": {
        "average": "5.0000",
        "count": 2
      },
      "reviews": {
        "total": 2,
        "per_page": 15,
        "current_page": 1,
        "last_page": 1,
        "from": 1,
        "to": 2,
        "data": [
          {
            "votes": null,
            "flags": null,
            "title": "David",
            "product_review_id": 2978412,
            "review": "...is beautiful and I had a great time on this tour. Already started to plan my next trip with them",
            "sku": "TSCB20",
            "rating": 5,
            "date_created": "",
            "order_id": "",
            "timeago": "",
            "reviewer": {
              "first_name": "David",
              "last_name": "",
              "verified_buyer": "yes",
              "address": "",
              "profile_picture": "",
              "gravatar": "38677a4e8a55189055d6e5bf2efa9ade"
            },
            "ratings": [],
            "replies": [],
            "images": [],
            "product": {
              "sku": "TSCB20",
              "name": "",
              "description": "",
              "link": "",
              "image_url": "",
              "mpn": "",
              "brand": null,
              "category": null,
              "custom": null
            },
            "author": {
              "email": "david@"
            }
          },
          {
            "votes": null,
            "flags": null,
            "title": null,
            "product_review_id": 2978009,
            "review": "Exceeded expectations on all fronts cultural and Food ",
            "sku": "TSCB20",
            "rating": 5,
            "date_created": "",
            "order_id": "RGFY4ZG",
            "timeago": "",
            "reviewer": {
              "first_name": "Alan",
              "last_name": "",
              "verified_buyer": "yes",
              "address": "",
              "profile_picture": "",
              "gravatar": "64e2ac644a158b76a82f9e1c5c2886f5"
            },
            "ratings": [],
            "replies": [],
            "images": [],
            "product": {
              "sku": "TSCB20",
              "name": "",
              "description": "",
              "link": "",
              "image_url": "",
              "mpn": "",
              "brand": null,
              "category": null,
              "custom": null
            },
            "author": {
              "email": ""
            }
          }
        ]
      },
      "ratings": [],
      "settings": {
        "write_review_button": 1,
        "disable_product_seo_css": 0,
        "show_product_review_titles": 0
      },
      "word": "Excellent",
      "products": [
        {
          "sku": "TSCB20",
          "name": ""
        }
      ],
      "write_review_link": ""
    }

我现在理想的结果是一个循环,获取评论者的姓名、评论本身和星级。例如

    <p class="review">Review here</p>
    <p class="reviewer">Name here</p>
    <p class="rating">Rating here</p>

所以从技术上来说,根据当前的输出,我应该有 2 条评论。由于我对 API 非常陌生,因此我们将非常感谢一些专家的帮助。提前非常感谢您。

php json api
2个回答
1
投票

简单

foreach()
就可以完成工作:

<?php foreach($data['reviews']['data'] as $dat){?>

    <p class="review"><?php echo $dat['review'];?></p>

    <p class="reviewer"><?php echo trim($dat['reviewer']['first_name'].' '.$dat['reviewer']['last_name']);?></p>

    <p class="rating"><?php echo $dat['rating'];?></p>
<?php }?>

0
投票

所以,让我告诉你我发现自己陷入的这种疯狂的情况。我在这家公司投入了大量资金,认为这将是一个赚外快的好机会。但后来事情变得更糟,突然我无法提取我的资金,除了询问我的个人信息之外,他们无法给我一个合理的理由,这有点奇怪。我吓坏了,直到一位朋友推荐 ULTIMATE RECOVERY FIRM 和他们出色的团队来帮助我。让我告诉你,他们是救星!他们不遗余力地从公司追回我的钱,经过一番周折,我终于拿回来了。我对他们的专业知识和坚持不懈地帮助我获得理应属于我的东西感激不尽。这绝对是一个关于在投资任何东西之前进行彻底研究的教训,这是肯定的!您可以通过电子邮件联系他们:[电子邮件受保护] WhatsApp:+1—(786)—432—2178

© www.soinside.com 2019 - 2024. All rights reserved.