PHP开发实现快递查询功能详解(2)

<?php
/**
 * Express.class.php 快递查询类 v1.0
 *
 * @copyright    百鬼夜行
 * @lastmodify    2017-01-19
 */
class Express
{
  //根据快递单号查询物流信息函数
  public function expressinfo($order){
    header('Content-Type:text/html; charset=utf-8');
    $result = $this -> getorder($order);
    $express = $this->traceExpress();
    foreach ($express as $key => $value){
      if ($result['com'] == $key){
        $result['comName'] = $value;
        break;
      }else{
        $result['comName'] = '暂无匹配';
      }
    }
    return $result;
  }

  //快递100物流公司
  public function traceExpress(){
    $express = array(
      'aae' => 'aae全球专递',
      'anjie' => '安捷快递',
      'anxindakuaixi' => '安信达快递',
      'biaojikuaidi' => '彪记快递',
      'datianwuliu' => '大田物流',
      'debangwuliu' => '德邦物流',
      'ems' => 'ems快递',
      'guotongkuaidi' => '国通快递',
      'huitongkuaidi' => '汇通快运',
      'jixianda' => '急先达',
      'kuaijiesudi' => '快捷速递',
      'quanfengkuaidi' => '全峰快递',
      'rufengda' => '如风达',
      'shentong' => '申通',
      'shunfeng' => '顺丰',
      'tiantian' => '天天快递',
      'xinfengwuliu' => '信丰物流',
      'yibangwuliu' => '一邦速递',
      'yuantong' => '圆通速递',
      'yunda' => '韵达快运',
      'zhaijisong' => '宅急送',
      'zhongtong' => '中通速递',
      'jd' => '京东快递'
    );
    return $express;
  }

  /*
   * 网页内容获取方法
  */
  private function getcontent($url)
  {
    if (function_exists("file_get_contents")) {
      $file_contents = file_get_contents($url);
    } else {
      $ch   = curl_init();
      $timeout = 5;
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
      $file_contents = curl_exec($ch);
      curl_close($ch);
    }
    return $file_contents;
  }

  /*
   * 获取对应名称和对应传值的方法
  */
  private function expressname($order)
  {
    $name  = json_decode($this->getcontent("http://www.kuaidi100.com/autonumber/auto?num={$order}"), true);
    $result = $name[0]['comCode'];
    if (empty($result)) {
      return false;
    } else {
      return $result;
    }
  }

  /*
   * 返回$data array   快递数组查询失败返回false
   * @param $order    快递的单号
   * $data['ischeck'] ==1 已经签收
   * $data['data']    快递实时查询的状态 array
  */
  public function getorder($order)
  {
    $keywords = $this->expressname($order);
    if (!$keywords) {
      return false;
    } else {
      $result = $this->getcontent("http://www.kuaidi100.com/query?type={$keywords}&postid={$order}");
      $data  = json_decode($result, true);
      return $data;
    }
  }
}

附录源代码>>>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/5787.html