简体   繁体   中英

How can I get a live data from API with cURL save or stream that data with another script?

I have a hardware which records data in every seconds. I can connect to the hardware with a browser and use the hardware's interface and get the live data. For example: I can call the realtime data with command like this:

http://192.168.100.120:2345/realtime 

That's what I can see in the browser:

DM_NumLogChans=5
DM_NumDataModes=1

DM_LogicalChan=1
DM_ChanType=SEQUENTIAL
DM_NumDims=2
DM_DataMode=1
DM_DataModeType=TIMHIS
DM_AxisLabel.Dim1=Time
DM_AxisLabel.Dim2=Value
DM_AxisUnits.Dim1=secs
DM_AxisUnits.Dim2=microstrain
DM_SampleRate=1.000000
DM_TimeBase=0.0
DM_ChanName=bridge_1
DM_UserMin=-583.220764
DM_UserMax=940.916199


DM_Start=
-439.779    -391.875    -680.114    1001.37 0
-442.068    -396.62 -680.945    1001.37 0
-443.571    -399.705    -680.639    1001.37 0
-445.598    -404.848    -684.662    1001.37 0

A new row appear in each seconds. I would like to get this data and save it to a file or display it in real time in my php program. How can I catch the data? I tried with cURL. I think that is the solution but I am really new to this. I would appreciate any help or advice you could give me.

Try this code with your url

function curl_download($Url){

    // is cURL installed yet?
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }

    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();

    // Now set some options (most are optional)

    // Set URL to download
    curl_setopt($ch, CURLOPT_URL, $Url);

    // Set a referer
    curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");

    // User agent
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");

    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    // Download the given URL, and return output
    $output = curl_exec($ch);

    // Close the cURL resource, and free system resources
    curl_close($ch);

    return $output;
}




echo curl_download("192.168.100.120:2345/realtime");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM