제품용 Reviews.io API의 Foreach 루프 (Foreach loop in Reviews.io API for Products)


문제 설명

제품용 Reviews.io API의 Foreach 루프 (Foreach loop in Reviews.io API for Products)

저는 API 세계를 처음 접했고 현재 특정 제품에 대한 내 review.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를 처음 접하기 때문에 전문가의 도움을 받아 주시면 감사하겠습니다. 미리 감사드립니다.


참조 솔루션

방법 1:

Simple foreach() will do the job:

<?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 }?>

(by sierraAnant ‑ Alive to die)

참조 문서

  1. Foreach loop in Reviews.io API for Products (CC BY‑SA 2.5/3.0/4.0)

#PHP #JSON #API






관련 질문

bash의 rsync가 php 생성 --exclude-from 파일을 구문 분석하지 않음 (rsync in bash not parsing php-generated --exclude-from file)

PHP 배열 값 저장 (PHP Save array value)

검색으로 배열에서 특정 데이터 가져오기 (get specific datas from a array by a search)

창 서비스를 사용하여 PHP 파일 트리거 (Trigger a php file using window service)

yii2 컨트롤러 작업 주입은 어떻게 작동합니까? (How does the yii2 Controller action injection works)

php와 drupal을 사용하여 pdf를 텍스트로 변환 (pdf to text convert using php and drupal)

PHP에서 카테고리 및 하위 카테고리 목록 검색 (Retrieve Category & Subcategory list in PHP)

PDO - COUNT(*) 결과를 얻습니까? (PDO - get the result of a COUNT(*)?)

PHP - MySQL 쿼리 문제 (PHP - Mysql query problem)

제품용 Reviews.io API의 Foreach 루프 (Foreach loop in Reviews.io API for Products)

숫자를 나누고 점 뒤에 하나의 숫자를 유지하십시오. (Split the number and keep one number after the dot)

내 메시지 입력이 데이터베이스에 들어가지 않습니다. (My message input doesn't get into database)







코멘트