1

I want to know how to explode or split xml and turn into array and then insert it to database. Because i have an api that need to hit every day and it return xml.

here's my xml sample:

<ArrayOfUnitPrice>
 <UnitPrice>
  <PriceAmount>1579.7080</PriceAmount>
  <PriceDate>2016-09-02</PriceDate>
  <PriceType>XWZ</PriceType>
 </UnitPrice>
 <UnitPrice>
  <PriceAmount>1028.4137</PriceAmount>
  <PriceDate>2016-09-02</PriceDate>
  <PriceType>ABC</PriceType>
 </UnitPrice>
 ...
</ArrayOfUnitPrice>

I'm using this code to extract the xml response:

$ch = curl_init("111.222.333.444:8080/code.asmx/Price");
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_POST,1);
      curl_setopt($ch, CURLOPT_TIMEOUT, 60);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$result=curl_exec ($ch);

//my code here

curl_close($ch);

SOLVED I already done solve my code by using this code below guys.

$xml = simplexml_load_string($result);

for ($i=0; $i < count($xml) ; $i++) {
  $arr[] = array (
  'PriceAmount' => $xml->UnitPrice[$i]->PriceAmount,
  'PriceDate' => $xml->UnitPrice[$i]->PriceDate,
  'PriceType' => $xml->UnitPrice[$i]->PriceType
  );
}
$data = json_decode(json_encode($arr), true);
$servername   = "localhost";
$username     = "root";
$password     = "";
$dbname       = "dailywork";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if(is_array($data)){
    $check = "SELECT * FROM table_unit";
    if($conn->query($check)->num_rows > 0){
      // my stuff to here :-D
    }else{
      /*Insert data to DB*/
      $sql = "INSERT INTO table_unit (PriceAmount, PriceDate, PriceType) values ";
      $valuesArr = array();
      foreach($data as $row){
          $PriceAmount  = $row[PriceAmount][0];
          $PriceDate    = $row[PriceDate][0];
          $PriceType    = $row[PriceType][0];
          $valuesArr[]  = "('$PriceAmount', '$PriceDate', '$PriceType')";
      }
      $sql .= implode(',', $valuesArr);
      if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
      } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
      }
      /*End Insert data to DB*/
    }
}

$conn->close();
2
  • Are you using a SOAP API? Commented Sep 5, 2016 at 11:13
  • @GrzegorzB.yes,thats right Commented Sep 6, 2016 at 2:28

1 Answer 1

1

This function will give you an array of your xml

function xml2php($xmlcontent) 
{
  $xml_parser = xml_parser_create();
  xml_parse_into_struct($xml_parser, $xmlcontent, $arr_vals);
  xml_parser_free($xml_parser);
  return $arr_vals;
}

pass your $result like this and check it

xml2php($result);

let me know if it helps you

Sign up to request clarification or add additional context in comments.

1 Comment

sorry dude, i wasn't able to understand your code so i use other code to solve it. but really appreciate your help. thanks @rahul

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.