How to convert xml response using curl into array then store in database using php -
i invoking php curl method on server , response xml type. curl saving output (after removing tags) in scalar type variable. there way store in database it's easy parse?
<response status="ok" responsetime="10"> <results> <wirelineservices> <blockfips>360775908001027</blockfips> <frn>0007556251</frn> <providername>time warner cable llc</providername> <doingbusinessas>time warner cable</doingbusinessas> <holdingcompanynumber>131352</holdingcompanynumber> <holdingcompanyname>time warner cable inc.</holdingcompanyname> <providerurl>www.timewarnercable.com</providerurl> <technologies> <technologycode>40</technologycode> <typicaldownloadspeed>0</typicaldownloadspeed> <typicaluploadspeed>0</typicaluploadspeed> <maximumadvertiseddownloadspeed>9</maximumadvertiseddownloadspeed> <maximumadvertiseduploadspeed>5</maximumadvertiseduploadspeed> <overallquality>1</overallquality> <speedquality>1.00</speedquality> <technologyquality>3.00</technologyquality> <providerquality>3.00</providerquality> <downloadquality>1.00</downloadquality> <uploadquality>1.00</uploadquality> <maximumproviderscore>3.00</maximumproviderscore> <maximumtechnologyscore>3.00</maximumtechnologyscore> <maximumspeedscore>2.00</maximumspeedscore> <maximumdownloadscore>2.00</maximumdownloadscore> <maximumuploadscore>2.00</maximumuploadscore> </technologies> </wirelineservices> <wirelineservices> <blockfips>360775908001027</blockfips> <frn>0003469442</frn> <providername>verizon new york inc.</providername> <doingbusinessas>verizon new york</doingbusinessas> <holdingcompanynumber>131425</holdingcompanynumber> <holdingcompanyname>verizon communications inc.</holdingcompanyname> <providerurl>www.connecttoverizon.com</providerurl> <technologies> <technologycode>10</technologycode> <typicaldownloadspeed>0</typicaldownloadspeed> <typicaluploadspeed>0</typicaluploadspeed> <maximumadvertiseddownloadspeed>5</maximumadvertiseddownloadspeed> <maximumadvertiseduploadspeed>3</maximumadvertiseduploadspeed> <overallquality>1</overallquality> <speedquality>1.00</speedquality> <technologyquality>2.00</technologyquality> <providerquality>2.00</providerquality> <downloadquality>0.00</downloadquality> <uploadquality>1.00</uploadquality> <maximumproviderscore>3.00</maximumproviderscore> <maximumtechnologyscore>2.00</maximumtechnologyscore> <maximumspeedscore>2.00</maximumspeedscore> <maximumdownloadscore>2.00</maximumdownloadscore> <maximumuploadscore>2.00</maximumuploadscore> </technologies> </wirelineservices> <broadbandsource> <statefips>36</statefips> <organization>the new york state office of cyber security</organization> <organizationurl>http://www.cscic.state.ny.us/broadband/</organizationurl> </broadbandsource> </results> </response>
my ans.
$curl = curl_init(); curl_setopt($curl, curlopt_url, $request_url); curl_setopt($curl, curlopt_timeout, 130); curl_setopt($curl, curlopt_returntransfer, 1); $response = curl_exec($curl); curl_close($curl); $xml = simplexml_load_string($response); $json = json_encode($xml); $arr = json_decode($json,true);
you this. iterate through arrays values of wirelineservices. result in 2 records. insert values in string , place string in array.
making query our db. asummed have column every value. , imloded array our string records in our db.
ofcourse need change table name , have use connection data.
<?php $xmlstr = '<response status="ok" responsetime="10"> <results> <wirelineservices> <blockfips>360775908001027</blockfips> <frn>0007556251</frn> <providername>time warner cable llc</providername> <doingbusinessas>time warner cable</doingbusinessas> <holdingcompanynumber>131352</holdingcompanynumber> <holdingcompanyname>time warner cable inc.</holdingcompanyname> <providerurl>www.timewarnercable.com</providerurl> <technologies> <technologycode>40</technologycode> <typicaldownloadspeed>0</typicaldownloadspeed> <typicaluploadspeed>0</typicaluploadspeed> <maximumadvertiseddownloadspeed>9</maximumadvertiseddownloadspeed> <maximumadvertiseduploadspeed>5</maximumadvertiseduploadspeed> <overallquality>1</overallquality> <speedquality>1.00</speedquality> <technologyquality>3.00</technologyquality> <providerquality>3.00</providerquality> <downloadquality>1.00</downloadquality> <uploadquality>1.00</uploadquality> <maximumproviderscore>3.00</maximumproviderscore> <maximumtechnologyscore>3.00</maximumtechnologyscore> <maximumspeedscore>2.00</maximumspeedscore> <maximumdownloadscore>2.00</maximumdownloadscore> <maximumuploadscore>2.00</maximumuploadscore> </technologies> </wirelineservices> <wirelineservices> <blockfips>360775908001027</blockfips> <frn>0003469442</frn> <providername>verizon new york inc.</providername> <doingbusinessas>verizon new york</doingbusinessas> <holdingcompanynumber>131425</holdingcompanynumber> <holdingcompanyname>verizon communications inc.</holdingcompanyname> <providerurl>www.connecttoverizon.com</providerurl> <technologies> <technologycode>10</technologycode> <typicaldownloadspeed>0</typicaldownloadspeed> <typicaluploadspeed>0</typicaluploadspeed> <maximumadvertiseddownloadspeed>5</maximumadvertiseddownloadspeed> <maximumadvertiseduploadspeed>3</maximumadvertiseduploadspeed> <overallquality>1</overallquality> <speedquality>1.00</speedquality> <technologyquality>2.00</technologyquality> <providerquality>2.00</providerquality> <downloadquality>0.00</downloadquality> <uploadquality>1.00</uploadquality> <maximumproviderscore>3.00</maximumproviderscore> <maximumtechnologyscore>2.00</maximumtechnologyscore> <maximumspeedscore>2.00</maximumspeedscore> <maximumdownloadscore>2.00</maximumdownloadscore> <maximumuploadscore>2.00</maximumuploadscore> </technologies> </wirelineservices> <broadbandsource> <statefips>36</statefips> <organization>the new york state office of cyber security</organization> <organizationurl>http://www.cscic.state.ny.us/broadband/</organizationurl> </broadbandsource> </results> </response>'; $xml = simplexml_load_string($xmlstr); $json = json_encode($xml); $arrays = json_decode($json,true); //this array looks echo "<pre>"; print_r($arrays); $counter = 0; // values inside wirelineservices foreach($arrays['results']['wirelineservices'] $nbr => $arr){ //start of input string $insert[$counter] = "("; //iterate through $arr foreach($arr $key => $value){ //if value array iterate through $value if(is_array($value)){ foreach($value $key2 => $value2){ $insert[$counter] .= "'" . $value2 . "', "; } } else { $insert[$counter] .= "'" . $value . "', "; } } //remove last ', ' string. $insert[$counter] = substr($insert[$counter], 0, -2) . ")"; //start new string $counter++; $sql = "insert `table` (`blockfips`, `frn`,`providername`,`doingbusinessas`,`holdingcompanynumber`,`holdingcompanyname`,`providerurl`,`technologycode`,`typicaldownloadspeed`,`typicaluploadspeed`,`maximumadvertiseddownloadspeed`, `maximumadvertiseduploadspeed`,`overallquality`,`speedquality`,`technologyquality`,`providerquality`,`downloadquality`,`uploadquality`,`maximumproviderscore`,`maximumtechnologyscore`,`maximumspeedscore`,`maximumdownloadscore`, `maximumuploadscore`) values " . implode(",",$insert); . ""; if (mysqli_query($connect, $sql)) { echo 'records created successfull'; } else { echo $sql . '"<br>"' . mysqli_error($connect); } ?>
Comments
Post a Comment