如何使用php将xml格式的内容插入数据库

时间:2019-05-10 07:31:41

标签: php xml

如何将这种类型的数据插入数据库?我要cityIdIsActiveCitycodeCountrycode一行一行地插入数据

{"DestinationCityList":"10379<\/CityId>1<\/IsActive> Chhota Shimla <\/CityName>IN<\/CountryCode><\/City>44807<\/CityId>1<\/IsActive>Abhaneri<\/CityName>IN<\/CountryCode><\/City>10631<\/CityId>1<\/IsActive>Abu<\/CityName>IN<\/CountryCode><\/City>44824<\/CityId>1<\/IsActive>Adimaly<\/CityName>IN<\/CountryCode><\/City>10677<\/CityId>1<\/IsActive>Agartala<\/CityName>IN<\/CountryCode><\/City>44745<\/CityId>1<\/IsActive>Agatti<\/CityName>IN<\/CountryCode><\/City>10380<\/CityId>1<\/IsActive>Agatti Island <\/CityName>IN<\/CountryCode><\/Cities>","Error":{"ErrorCode":0,"ErrorMessage":""}

代码:

<?php

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => "some url",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => "{ 
            \"TokenId\": \"token_id\",
            \"ClientId\": \"client_id\",
            \"EndUserIp\": \"ip_address\",
            \"CountryCode\": \"IN\"
        } ",
        CURLOPT_HTTPHEADER => array(
        "cache-control: no-cache",
        "content-type: application/json",
        "postman-token: 6e10989f-ecef-a6c6-92b8-a13504c162eb"
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
        $data=json_decode($response,true);

        $get = $data['DestinationCityList'];
        echo "<pre>";print_r(explode(' ', $get));//Convert and print value here



    //foreach ($data['DestinationCityList'] as $key => $value) 
    //{
        //$city_id = $value['CityId'];
        //$city_name = $value['CityName'];
        //$country_code = $value['CountryCode'];
        //$SQL = DB::query("INSERT INTO tbl_city_list(fld_city_id, fld_city_name, fld_country_code, fld_active) VALUES ('$city_id','$city_name','$country_code','1'");
    //}       
    //}
?>

0 个答案:

没有答案