方法如下:
/** * 根据商品ID获取至广州的'快递'邮费 * @param type $id * @return type 邮费 * author uuecs.Com */ function get_fee_by_id($id) { $url = "http://delivery.taobao.com/detail/delivery_detail.do?itemId={$id}&areaId=440100";//广州邮编 $contents = file_get_contents($url); $contents = iconv("gbk", "utf-8", $contents); $ret_price = FALSE; //如果返回不为空,则数据正确,进行提取 if (!empty($contents)) { preg_match("/(?:'postDesc':\{'default':')(.*)(?:\'}\}+)/i", $contents, $matches); $need = $matches[1]; $need = trim($need); $price_fee = 0; //如果 '卖家承担运费' ,则邮费为0 if (strpos($need, "卖家承担运费") !== FALSE) { $price_fee = 0; } else { $tem_ = preg_replace("/[\s]*(EMS.*)/i", '', $need); if (strpos($tem_, '快递') !== FALSE) {//如果是 '快递' 项 $tem_ = str_replace('快递:', '', $tem_); if (strpos($tem_, '免运费') !== FALSE) {//如果是 '免运费' 项 $price_fee = 0; } else {//如果有 价格, 则提取 preg_match("/([\d]+\.?[\d]+)/", $tem_, $matches_); $price_fee = $matches_[1]; } } } $ret_price = floatval($price_fee); } return $ret_price; }