Home > Back-end >  JSON data using foreach loop in php and save to database
JSON data using foreach loop in php and save to database

Time:05-21

I get a list of invoices through api. See:

<?php
$url = "https://app.domain.tld/api/v2/accounts/censored/invoices.json?status=uncollectible";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
    "User-Agent: YourApp ([email protected])",
    "Authorization: Basic censored==",
);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
echo $resp = curl_exec($curl);
curl_close($curl);
?>

This data will be returned to me in JSON:

[{
    "id": 23487088,
    "custom_id": null,
    "proforma": false,
    "partial_proforma": false,
    "number": "2022-05-0008",
    "number_format_id": 363568,
    "variable_symbol": "2022050008",
    "your_name": "Censored s.r.o.",
    "your_street": "Street 1801/22",
    "your_street2": null,
    "your_city": "Praha",
    "your_zip": "10900",
    "your_country": "CZ",
    "your_registration_no": "00055222",
    "your_vat_no": "CZ00055222",
    "your_local_vat_no": null,
    "client_name": "Firma s.r.o.",
    "client_street": "Street 508/2",
    "client_street2": null,
    "client_city": "Prague",
    "client_zip": "111 22",
    "client_country": "CZ",
    "client_registration_no": "0000000",
    "client_vat_no": "",
    "client_local_vat_no": "",
    "subject_id": 14471217,
    "subject_custom_id": null,
    "generator_id": null,
    "related_id": null,
    "correction": false,
    "correction_id": null,
    "paypal": false,
    "gopay": false,
    "token": "zuqpytRSBg",
    "status": "uncollectible",
    "order_number": "",
    "issued_on": "2022-05-13",
    "taxable_fulfillment_due": "2022-05-13",
    "due": 7,
    "due_on": "2022-05-20",
    "sent_at": null,
    "paid_at": null,
    "reminder_sent_at": null,
    "accepted_at": null,
    "cancelled_at": null,
    "webinvoice_seen_at": null,
    "note": "",
    "footer_note": "Společnost je zapsána v obchodním rejstříku vedeném Městským soudem v Praze oddíl C, vložka 000000.",
    "private_note": null,
    "tags": [],
    "bank_account": "0000000000/2010",
    "iban": "CZ000000000000000000000",
    "swift_bic": "FIOBCZPPXXX",
    "show_already_paid_note_in_pdf": false,
    "payment_method": "bank",
    "hide_bank_account": false,
    "currency": "CZK",
    "exchange_rate": "1.0",
    "language": "cz",
    "transferred_tax_liability": false,
    "eu_electronic_service": false,
    "oss": "disabled",
    "vat_price_mode": "without_vat",
    "supply_code": "",
    "subtotal": "3000.0",
    "total": "3630.0",
    "native_subtotal": "3000.0",
    "native_total": "3630.0",
    "remaining_amount": "3630.0",
    "remaining_native_amount": "3630.0",
    "paid_amount": "0.0",
    "eet": false,
    "eet_cash_register": null,
    "eet_store": null,
    "eet_records": [],
    "lines": [{
        "id": 52921163,
        "name": "IT services",
        "quantity": "1.0",
        "unit_name": "",
        "unit_price": "3000.0",
        "vat_rate": 21,
        "unit_price_without_vat": "3000.0",
        "unit_price_with_vat": "3630.0"
    }],
    "attachment": null,
    "html_url": "https://app.fakturoid.cz/censored/invoices/23487088",
    "public_html_url": "https://app.fakturoid.cz/censored/p/zuqpytRSBg/2022-05-0008",
    "url": "https://app.fakturoid.cz/api/v2/accounts/censored/invoices/23487088.json",
    "pdf_url": "https://app.fakturoid.cz/api/v2/accounts/censored/invoices/23487088/download.pdf",
    "subject_url": "https://app.fakturoid.cz/api/v2/accounts/censored/subjects/14471217.json",
    "created_at": "2022-05-21T16:27:32.488 02:00",
    "updated_at": "2022-05-21T16:27:35.464 02:00"
}, {
    "id": 23487082,
    "custom_id": null,
    "proforma": false,
    "partial_proforma": false,
    "number": "2022-05-0007",
    "number_format_id": 363568,
    "variable_symbol": "2022050007",
    "your_name": "censored s.r.o.",
    "your_street": "censored",
    "your_street2": null,
    "your_city": "Praha",
    "your_zip": "10900",
    "your_country": "CZ",
    "your_registration_no": "00055222",
    "your_vat_no": "CZ00055222",
    "your_local_vat_no": null,
    "client_name": "Firma s.r.o.",
    "client_street": "Street 508/2",
    "client_street2": null,
    "client_city": "Prague",
    "client_zip": "111 22",
    "client_country": "CZ",
    "client_registration_no": "0000000",
    "client_vat_no": "",
    "client_local_vat_no": "",
    "subject_id": 14471217,
    "subject_custom_id": null,
    "generator_id": null,
    "related_id": null,
    "correction": false,
    "correction_id": null,
    "paypal": false,
    "gopay": false,
    "token": "7xIDMf64iQ",
    "status": "uncollectible",
    "order_number": "",
    "issued_on": "2022-05-04",
    "taxable_fulfillment_due": "2022-05-04",
    "due": 1,
    "due_on": "2022-05-05",
    "sent_at": null,
    "paid_at": null,
    "reminder_sent_at": null,
    "accepted_at": null,
    "cancelled_at": null,
    "webinvoice_seen_at": null,
    "note": "",
    "footer_note": "Společnost je zapsána v obchodním rejstříku vedeném Městským soudem v Praze oddíl C, vložka 000000.",
    "private_note": null,
    "tags": [],
    "bank_account": "000000000/2010",
    "iban": "CZ000000000000000000000",
    "swift_bic": "FIOBCZPPXXX",
    "show_already_paid_note_in_pdf": false,
    "payment_method": "bank",
    "hide_bank_account": false,
    "currency": "CZK",
    "exchange_rate": "1.0",
    "language": "cz",
    "transferred_tax_liability": false,
    "eu_electronic_service": false,
    "oss": "disabled",
    "vat_price_mode": "without_vat",
    "supply_code": "",
    "subtotal": "5000.0",
    "total": "6050.0",
    "native_subtotal": "5000.0",
    "native_total": "6050.0",
    "remaining_amount": "6050.0",
    "remaining_native_amount": "6050.0",
    "paid_amount": "0.0",
    "eet": false,
    "eet_cash_register": null,
    "eet_store": null,
    "eet_records": [],
    "lines": [{
        "id": 52921141,
        "name": "IT services",
        "quantity": "1.0",
        "unit_name": "",
        "unit_price": "5000.0",
        "vat_rate": 21,
        "unit_price_without_vat": "5000.0",
        "unit_price_with_vat": "6050.0"
    }],
    "attachment": null,
    "html_url": "https://app.fakturoid.cz/censored/invoices/23487082",
    "public_html_url": "https://app.fakturoid.cz/censored/p/7xIDMf64iQ/2022-05-0007",
    "url": "https://app.fakturoid.cz/api/v2/accounts/censored/invoices/23487082.json",
    "pdf_url": "https://app.fakturoid.cz/api/v2/accounts/censored/invoices/23487082/download.pdf",
    "subject_url": "https://app.fakturoid.cz/api/v2/accounts/censored/subjects/14471217.json",
    "created_at": "2022-05-21T16:25:51.916 02:00",
    "updated_at": "2022-05-21T16:26:54.323 02:00"
}]

The problem is that I can't throw it into a nice boostrap table, columns using PHP and possibly save it to a database. I tried it this way, but I did not meet with success.

<?php
    $data = json_decode($resp->data, true);
     foreach ($data as $key => $value) {
     $invoice_id = $value['array']['id']; // id invoice
     $invoice_status = $value['array']['status']; // paid, unpaid, uncollectible etc...
     $invoice_price = $value['array']['total']; // total price with VAT
}
?>

But I'm not successful. I'm getting a error: Error: Trying to get property 'data' of non-object

I'm at the end, please help

CodePudding user response:

please use this php code, your array contains objects


$data = json_decode($resp); 
foreach ($data as $key => $value) {
    $invoice_id = $value->id; // id invoice
    $invoice_status = $value->status; // paid, unpaid, uncollectible etc...
    $invoice_price = $value->total; // total price with VAT
}
  • Related