Uutiset:

22.03.2025
FinWX:n ja Ilmatieteen laitoksen välinen säädatan vastaanottopilotti on päättynyt

Säädataa siirtävä datasilta ajettiin alas 22.03.2025 kello 02:00. (22.03.2025, 00:00 UTC).

Ilmatieteen laitoksen terveiset asian tiimoilta voitte lukea täältä.

Main Menu

parsia

Aloittaja ARu, tiistai, 06.05.2025, 17:17

« edellinen - seuraava »

0 Jäsenet ja 1 Vieras katselee tätä aihetta.

ARu

Tälläinen php sivu , joka hakee kyselyllä tiedot.



pv.PNG


Miten saan parsittua ainoastaan noi punaisella merkityt kohdat toiselle sivulle?

Ei oikein toi koodaustaito riitä.

ARu

Tällaisella koodilla haettu noi edellisen kuvan tiedot.

Voisko sen parsia jo tässävaiheessa kun hakee tietoja?

iccb

Pitäisi kyllä nähdä se kohdesivun koko rakenne. Äkkiseltään näyttäisi xml tai json koodilta toi kuva?
Kannattaa kysellä chatgpl:tä vinkkejä myöskin.

khyron

Huvikseni kysyin co-pilotilta koska en php:tä varsinaisesti osaa.

// Decode JSON into an associative array
$data = json_decode($json, true);

// Initialize variables to store extracted values
$aFo1 = null;
$etdyGe1 = null;

// Loop through the dataList to find the desired keys
foreach ($data["dataList"] as $item) {
    if ($item["key"] === "A_Fo1") {
        $aFo1 = $item["value"];
    } elseif ($item["key"] === "Etdy_ge1") {
        $etdyGe1 = $item["value"];
    }
}

// Output results
echo "A_Fo1: " . $aFo1 . " Hz\n";
echo "Etdy_ge1: " . $etdyGe1 . " kWh\n";

Tossa toki tehdään oletus että kyse olis solarman apin tuloksesta ja löytämäni dokumentin esimerkki on vielä validi.

khyron

Huvikseen piti vielä kysyä osaisko co-pilot tehdä suoraan sopivan scriptin, ei ihan menny putkeen kun melkeen tiesi että jonkunlainen laitteen sarjanumero pitäs olla pyynnössä, mutta ei lopuksikaan osannu laittaa sitä oikein:) Boldasin promptit että on edes suunnilleen selkee.

LainaaCan you give me a php script that fetches data from solarman api and print Hz and kWh values from the reply?
Certainly! Below is a PHP script that fetches data from the Solarman API and extracts Hz (frequency) and kWh (energy production) values from the response.

php
<?php
// Solarman API URL
$apiUrl = "https://api.solarmanpv.com/v1/device/data"; // Replace with the correct endpoint

// API Key and Device Information (Modify accordingly)
$apiKey = "YOUR_API_KEY"; // Replace with your Solarman API key
$deviceId = "YOUR_DEVICE_ID"; // Replace with your actual device ID

// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "Authorization: Bearer $apiKey"
]);

// Execute request and get response
$response = curl_exec($ch);
curl_close($ch);

// Decode JSON response
$data = json_decode($response, true);

// Check if data is valid
if (isset($data["dataList"])) {
    $hzValue = null;
    $kwhValue = null;

    // Iterate through the data list and extract relevant values
    foreach ($data["dataList"] as $item) {
        if ($item["key"] === "A_Fo1") {
            $hzValue = $item["value"];
        } elseif ($item["key"] === "Etdy_ge1") {
            $kwhValue = $item["value"];
        }
    }

    // Print results
    echo "AC Output Frequency: " . ($hzValue ? $hzValue . " Hz" : "Not found") . "\n";
    echo "Daily Production: " . ($kwhValue ? $kwhValue . " kWh" : "Not found") . "\n";
} else {
    echo "Failed to retrieve valid data from Solarman API.\n";
}
?>
Explanation:
cURL is used to make a request to the Solarman API.

API key and device ID need to be replaced with your own credentials.

The script parses the JSON response to extract A_Fo1 (frequency in Hz) and Etdy_ge1 (daily production in kWh).

Results are printed in a readable format.

Let me know if you need further improvements or adjustments! 😊

Where is variable $deviceId used?
You're right to question that—the $deviceId variable isn't actually used in the script!

While it's declared, there's no reference to it in the API request. If the Solarman API requires the deviceId, you'd need to append it to the request URL or include it in the request payload.

For example, if the API URL expects a query parameter:

php
$apiUrl = "https://api.solarmanpv.com/v1/device/data?deviceId=" . $deviceId;
Or if it's required in the JSON request body:

php
$payload = json_encode(["deviceId" => $deviceId]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
Good catch! If deviceId is necessary, be sure to include it properly. Let me know if you need help refining the request!

ARu

#5
Täytin kohdat omilla tiedoilla ja sivu antaa vastaukseksi:
Failed to retrieve valid data from Solarman API.

Joo sarjanumero ja token (pitkä numerosarja) pitäis olla myös mukana.