外卖库 xml 解析 以及坐标baidu 转换

<?php
include "image.php";
$con = mysql_connect("127.0.0.1:3307", 'waimai', 'waimai');
mysql_select_db("waimai", $con);
mysql_query('set names utf8', $con);
$file=glob("/data/web/zhua/*.xml");
foreach($file as $spco=>$val){
   $data=getinfo($val);
   $sql=getsql($data);
   if (mysql_query($sql)) {
      unset($data['combination']);
      print_r($data);
      echo '成功插入'.$spco.'条数据'."\n";
   } else {
      echo $sql,"插入失败了..."; exit;
   }
}

function getsql($data){
    $sql = "insert into wm_content set ";
    foreach ($data as $key => $val) {
        $sql .= "$key='" . mysql_real_escape_string($val) . "',";
    }
    return $sql .= "status=1";
}


function getinfo($file) {
    $doc = new DOMDocument();
    $doc->load($file); //读取xml文件
    $humans = $doc->getElementsByTagName("data"); //取得humans标签的对象数组
    foreach ($humans as $key => $human) {
        $data['remark'] = '外卖库|'.$human->getElementsByTagName("url")->item(0)->nodeValue;
        $data['name'] = $human->getElementsByTagName("name")->item(0)->nodeValue;
        $data['area'] = $human->getElementsByTagName("area")->item(0)->nodeValue;
        $data['address'] = $human->getElementsByTagName("address")->item(0)->nodeValue;
        $data['stmoney'] = $human->getElementsByTagName("stmoney")->item(0)->nodeValue;
        $data['time_one'] = $human->getElementsByTagName("opentime")->item(0)->nodeValue;
        $comb = $human->getElementsByTagName("combination")->item(0)->nodeValue;
        $oudata = json_decode(str_replace(array ('<![CDATA','>'), '', $comb), true);
        $data['combination'] = base64_encode(json_encode($oudata));
        $phone = $human->getElementsByTagName("mbphone")->item(0)->nodeValue;
        $data['mbphone'] = getphone($phone);
        $lan = getlatlong($data['address']);
        $data['latitude'] = $lan['lat'];
        $data['longitude'] = $lan['long'];
        $data['source']=1;
        return $data;
    }
}
function getphone($imgPath) {
    $gjPhone = new gjPhone($imgPath);
    $gjPhone->getHec();
    $horData = $gjPhone->magHorData();
    $verData = $gjPhone->magVerData($horData);
    $phone = $gjPhone->showPhone($verData);
    if (strstr($phone, "/"))
        $phone = $gjPhone->showPhone($verData, 2);
    return $phone ? $phone : $imgPath;
}
function getlatlong($address = '') {
    $key = 'b64856ba2cabeef3e5ef963ad7f87d68';
    $url = "http://api.map.baidu.com/geocoder?address=$address&output=json&key=$key&city=北京";
    $file = file_get_contents($url);
    $out = json_decode($file, true);
    if (isset($out['result']['location']['lng'])) {
        $latlong['lat'] = $out['result']['location']['lng'];
        $latlong['long'] = $out['result']['location']['lng'];
    } else {
        $latlong['lat'] = '';
        $latlong['long'] = '';
    }
    return $latlong;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值