PHP: JSON in CSV konvertieren

canju

Mitglied
OK, dann passt es ja erstmal soweit.
Habe zur Sicherheit max_execution_time und max_input_time jeweils auf 600 gesetzt.
 

canju

Mitglied
Habe doch noch eine Frage. Ich verwende die json Datei in einem ETL Service, der - so vermute ich - Probleme mit der ersten und /oder zweiten eckigen Klammer in der response hat.
JSON:
[
    [
        {
            "id": 6565465,
            "url": "http:\/\/www.example.me",
            "advertiserId": 644554,
            "publisherId": 8864456,
            "commissionSharingPublisherId": null,
            "commissionSharingSelectedRatePublisherId": null,
            "campaign": null,
            "siteName": "http:\/\/www.example.me",
            "commissionStatus": "declined",
            "commissionAmount": {
                "amount": 432,
                "currency": "EUR"
            },
            "saleAmount": {
                "amount": 1,
                "currency": "EUR"
            },
            "ipHash": "-34343435454453",
            "customerCountry": "DE",
            "clickRefs": {
                "clickRef": "rerere445454"
            },
            "clickDate": "2020-11-19T16:52:00",
            "transactionDate": "2020-11-19T16:52:00",
            "validationDate": "2021-04-26T17:04:00",
            "type": "Commission group transaction",
            "declineReason": "kein Vertrag zustande gekommen",
            "voucherCodeUsed": false,
            "voucherCode": null,
            "lapseTime": 33,
            "amended": false,
            "amendReason": null,
            "oldSaleAmount": null,
            "oldCommissionAmount": null,
            "clickDevice": "Apple Mac",
            "transactionDevice": "Apple Mac",
            "customerAcquisition": null,
            "publisherUrl": null,
            "advertiserCountry": "DE",
            "orderRef": null,
            "customParameters": [
                {
                    "key": "1",
                    "value": "ebfc5c38-9711-4def-932e-64fgthtger456iio7+443303"
                }
            ],
            "transactionParts": [
                {
                    "commissionGroupId": 693456,
                    "amount": 1,
                    "commissionAmount": 432,
                    "advertiserCost": null,
                    "commissionGroupCode": "commissionGroupCode",
                    "commissionGroupName": "commissionGroupName",
                    "trackedParts": [
                        {
                            "code": "code",
                            "amount": 1,
                            "currency": "EUR"
                        }
                    ]
                }
            ],
            "paidToPublisher": false,
            "paymentId": 0,
            "transactionQueryId": 0,
            "originalSaleAmount": null,
            "advertiserCost": {
                "amount": null,
                "currency": null
            },
            "basketProducts": null
        }
    ]
]

kriegen wir die noch weg, sodass das Ergebnis dann so:

JSON:
[
    {
        "id": 6565465,
        "url": "http:\/\/www.example.me",
        "advertiserId": 644554,
        "publisherId": 8864456,
        "commissionSharingPublisherId": null,
        "commissionSharingSelectedRatePublisherId": null,
        "campaign": null,
        "siteName": "http:\/\/www.example.me",
        "commissionStatus": "declined",
        "commissionAmount": {
            "amount": 432,
            "currency": "EUR"
        },
        "saleAmount": {
            "amount": 1,
            "currency": "EUR"
        },
        "ipHash": "-34343435454453",
        "customerCountry": "DE",
        "clickRefs": {
            "clickRef": "rerere445454"
        },
        "clickDate": "2020-11-19T16:52:00",
        "transactionDate": "2020-11-19T16:52:00",
        "validationDate": "2021-04-26T17:04:00",
        "type": "Commission group transaction",
        "declineReason": "kein Vertrag zustande gekommen",
        "voucherCodeUsed": false,
        "voucherCode": null,
        "lapseTime": 33,
        "amended": false,
        "amendReason": null,
        "oldSaleAmount": null,
        "oldCommissionAmount": null,
        "clickDevice": "Apple Mac",
        "transactionDevice": "Apple Mac",
        "customerAcquisition": null,
        "publisherUrl": null,
        "advertiserCountry": "DE",
        "orderRef": null,
        "customParameters": [
            {
                "key": "1",
                "value": "ebfc5c38-9711-4def-932e-64fgthtger456iio7+443303"
            }
        ],
        "transactionParts": [
            {
                "commissionGroupId": 693456,
                "amount": 1,
                "commissionAmount": 432,
                "advertiserCost": null,
                "commissionGroupCode": "commissionGroupCode",
                "commissionGroupName": "commissionGroupName",
                "trackedParts": [
                    {
                        "code": "code",
                        "amount": 1,
                        "currency": "EUR"
                    }
                ]
            }
        ],
        "paidToPublisher": false,
        "paymentId": 0,
        "transactionQueryId": 0,
        "originalSaleAmount": null,
        "advertiserCost": {
            "amount": null,
            "currency": null
        },
        "basketProducts": null
    }
]

oder so:

JSON:
{
    "id": 6565465,
    "url": "http:\/\/www.example.me",
    "advertiserId": 644554,
    "publisherId": 8864456,
    "commissionSharingPublisherId": null,
    "commissionSharingSelectedRatePublisherId": null,
    "campaign": null,
    "siteName": "http:\/\/www.example.me",
    "commissionStatus": "declined",
    "commissionAmount": {
        "amount": 432,
        "currency": "EUR"
    },
    "saleAmount": {
        "amount": 1,
        "currency": "EUR"
    },
    "ipHash": "-34343435454453",
    "customerCountry": "DE",
    "clickRefs": {
        "clickRef": "rerere445454"
    },
    "clickDate": "2020-11-19T16:52:00",
    "transactionDate": "2020-11-19T16:52:00",
    "validationDate": "2021-04-26T17:04:00",
    "type": "Commission group transaction",
    "declineReason": "kein Vertrag zustande gekommen",
    "voucherCodeUsed": false,
    "voucherCode": null,
    "lapseTime": 33,
    "amended": false,
    "amendReason": null,
    "oldSaleAmount": null,
    "oldCommissionAmount": null,
    "clickDevice": "Apple Mac",
    "transactionDevice": "Apple Mac",
    "customerAcquisition": null,
    "publisherUrl": null,
    "advertiserCountry": "DE",
    "orderRef": null,
    "customParameters": [
        {
            "key": "1",
            "value": "ebfc5c38-9711-4def-932e-64fgthtger456iio7+443303"
        }
    ],
    "transactionParts": [
        {
            "commissionGroupId": 693456,
            "amount": 1,
            "commissionAmount": 432,
            "advertiserCost": null,
            "commissionGroupCode": "commissionGroupCode",
            "commissionGroupName": "commissionGroupName",
            "trackedParts": [
                {
                    "code": "code",
                    "amount": 1,
                    "currency": "EUR"
                }
            ]
        }
    ],
    "paidToPublisher": false,
    "paymentId": 0,
    "transactionQueryId": 0,
    "originalSaleAmount": null,
    "advertiserCost": {
        "amount": null,
        "currency": null
    },
    "basketProducts": null
}

aussieht?
 

Sempervivum

Erfahrenes Mitglied
Statt die Resultate einfach hinter einander zu hängen, müssen wir die Arrays zusammen führen durch die Funktion array_merge:
PHP: array_merge - Manual
(Ein Kommentar dort sagt, dass das Anhängen mit einer Schleife um Längen schneller ist aber um den Code kurz zu halten habe ich mal array_merge verwendet).
Code:
$bearer_token = 'token';
//API KEY AND PASSWORD
$headers = array("Authorization: Bearer $bearer_token", 'Content-Type: application/json');
$current_datetime = date("Y-m-d H:i:s");
$script_name = $_SERVER['SCRIPT_NAME'];
$dateObj = new DateTime();
$dateObj->modify('-12 months');
$responseArr = [];
for ($i = 0; $i < 13; $i++) {
    // prepare start and end time and URL:
    $start_date = $dateObj->format('Y-m-d');
    $dateObj->modify('+30 days');
    $end_date = $dateObj->format('Y-m-d');
    $url = 'https://webentwicklung.ulrichbangert.de/testget-json-2.php?startDate=' . $start_date . 'T00%3A00%3A00&endDate=' . $end_date . 'T00%3A00%3A00&timezone=Europe/Berlin&dateType=transaction';
    echo 'url=' . $url . '<br>';
    // fetch from API by use of curl:
    $ch = curl_init($url) or die($current_datetime . " - " . $script_name . " - Couldnt reach endpoint \n");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $data = curl_exec($ch);
    echo 'response=' . $data . '<br>';
    $data = json_decode($data, true);
    $responseArr = array_merge($responseArr, $data);
}
echo ('<pre>');
var_dump($responseArr);
echo ('<pre>');
file_put_contents('test-get-json.json', json_encode($responseArr, JSON_PRETTY_PRINT));
 

Neue Beiträge