当我将json导入到csv(php)时,结果很混乱

时间:2016-12-08 20:02:58

标签: php json excel csv airbnb

我正在尝试获取airbnb的数据,为此,我与airbnb api联系,但我的问题是转换为csv。我在Linux中编译了代码,并将结果保存在csv文件中。有人知道如何在没有数据混乱的情况下做到这一点吗?感谢。

Excel file

<?php
    $limit = 50;
    For($i=0, $offset = 0; $i < 100; $i++, $offset += $limit){

        $url="https://api.airbnb.com/v2/search_results?client_id=3092nxybyb0otqw18e8nh5nty&locale=es-ES&currency=EUR&_format=for_search_results&_limit=$limit&_offset=$offset&fetch_facets=true&guests=1&ib=false&ib_add_photo_flow=true&location=Las-Palmas-de-Gran-Canaria--Espa%25C3%25B1a&min_bathrooms=1&min_bedrooms=1&min_beds=1&min_num_pic_urls=0&price_max=1000&price_min=9&sort=1&user_lat=28.1132&user_lng=-15.4409";

        $ci = curl_init();
        curl_setopt_array( $ci, array(CURLOPT_URL => $url,CURLOPT_TIMEOUT => 3600, CURLOPT_RETURNTRANSFER => true));
        $result = curl_exec($ci);
        curl_close($ci);

        $data = json_decode($result, true);
        if(count($data['search_results']) == 0) die("No more listings");

        foreach($data['search_results'] as $d) {

            $id = $d['listing']['id'];

            $urlita = "https://api.airbnb.com/v2/reviews?client_id=3092nxybyb0otqw18e8nh5nty&listing_id=$id&role=all";

            $cii = curl_init();
            curl_setopt_array( $cii, array(CURLOPT_URL => $urlita,CURLOPT_TIMEOUT => 3600, CURLOPT_RETURNTRANSFER => true));
            $reviewResult = curl_exec($cii);
            curl_close($cii);

            $review = json_decode($reviewResult, true);

            foreach($review['reviews'] as $r){
                echo $r['author']['acceptance_rate'] . ";";
                echo $r['author']['created_at'] . ";";
                echo $r['author']['first_name'] . ";";
                echo $r['author']['has_profile_pic'] . ";";
                echo $r['author']['id'] . ";";
                echo $r['author']['identity_mt_verified'] . ";";
                echo $r['author']['identity_v2_verified'] . ";";
                echo $r['author']['identity_verified'] . ";";
                echo $r['author']['is_superhost'] . ";";
                echo $r['author']['is_trip_host'] . ";";
                echo $r['author']['picture_large_url'] . ";";
                echo $r['author']['picture_url'] . ";";
                echo $r['author']['recommendation_count'] . ";";
                echo $r['author']['response_rate'] . ";";
                echo $r['author']['response_time'] . ";";
                echo $r['author']['reviewee_count'] . ";";
                echo $r['author']['thumbnail_medium_url'] . ";";
                echo $r['author']['thumbnail_url'] . ";";

                echo $r['author_id'] . ";";
                echo $r['can_be_edited'] . ";";
                echo $r['comments'] . ";";
                echo $r['created_at'] . ";";
                echo $r['id'] . ";";
                echo $r['listing_id'] . ";";
                echo $r['recipient_id'] . ";";

                echo "\n";
            }
        }
    }

0 个答案:

没有答案