Search code examples
phpfunctionparameterscontrollerdata-processing

How to receive data of below mentioned type and extract it


I am trying insert the values that has been continuously provided by the data provider and store it into table . The below mentioned is the format:

GO XXX,YYY,ZZZ=Ss
            %<QUOTE symbol="XXX" name="testname" exchange="OTHER" basecode="5" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212104600" open="67" high="67" low="64" last="64" previous="59" volume="49000" numtrades="3" pricevolume="322.70" id="combined"/><SESSION day="A" timestamp="20171211000000" last="59" id="previous"/></QUOTE>
            %<QUOTE symbol="YYY" name="testname2" exchange="OTHER" basecode="0" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212102000" open="169" high="169" low="161" last="169" previous="169" volume="60309" numtrades="6" pricevolume="1009.82" id="combined"/><SESSION day="A" timestamp="20171211000000" last="169" id="previous"/></QUOTE>
            %<QUOTE symbol="ZZZ" name="testname3" exchange="OTHER" basecode="9" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212105500" open="8" high="8" low="6" last="6" previous="8" volume="9799179" numtrades="22" pricevolume="7013.11" id="combined"/><SESSION day="A" timestamp="20171211000000" last="8" id="previous"/></QUOTE>

How to pass this data to controller function and extract each and every values such as 'symbol','name' etc...?

Please help me to solve this...


Solution

  • Using SimpleXML (after removing the % - assuming this is part of the data), you can easily get at the attributes...

    $data = <<< XML
    GO XXX,YYY,ZZZ=Ss
                %<QUOTE symbol="XXX" name="testname" exchange="OTHER" basecode="5" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212104600" open="67" high="67" low="64" last="64" previous="59" volume="49000" numtrades="3" pricevolume="322.70" id="combined"/><SESSION day="A" timestamp="20171211000000" last="59" id="previous"/></QUOTE>
                %<QUOTE symbol="YYY" name="testname2" exchange="OTHER" basecode="0" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212102000" open="169" high="169" low="161" last="169" previous="169" volume="60309" numtrades="6" pricevolume="1009.82" id="combined"/><SESSION day="A" timestamp="20171211000000" last="169" id="previous"/></QUOTE>
                %<QUOTE symbol="ZZZ" name="testname3" exchange="OTHER" basecode="9" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212105500" open="8" high="8" low="6" last="6" previous="8" volume="9799179" numtrades="22" pricevolume="7013.11" id="combined"/><SESSION day="A" timestamp="20171211000000" last="8" id="previous"/></QUOTE>
    
    XML;
    
    preg_match_all("/%<QUOTE(.*?)<\/QUOTE>/", $data, $list);
    foreach ( $list[0] as $item )  {
        $item = ltrim($item, "%");
        $xml = simplexml_load_string($item);
        echo (string)$xml['exchange'].PHP_EOL;
        foreach ( $xml->SESSION as $session )   {
            echo $session['day'].'-'.$session['timestamp'].PHP_EOL;
        }
    }
    

    The above code outputs...

    OTHER
    B-20171212104600
    A-20171211000000
    OTHER
    B-20171212102000
    A-20171211000000
    OTHER
    B-20171212105500
    A-20171211000000
    

    I've updated the code with your new data structure, I'm using regular expressions - which I'm not a huge fan of, but this should extract just the components you need. It uses the same code structure once it's extracted each individual item as before.

    To insert the data into a database, you need to look into something like PDO and how to add data to a database (anything like https://phpdelusions.net/pdo#dml will help).