PHP快速抓取快递信息

<?php
header("Content-type:text/html;charset=utf-8");
/**
 *  Express.class.php   快递查询类
 * @copyright            chzeze
 * @lastmodify            2015-10-28
 */

class Express {
     
    private $expressname =array(); //封装了快递名称
    
    function __construct(){
        $this->expressname = $this->expressname();
    }
    
    /*
     * 采集网页内容的方法
     */
    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(){
        $result = $this->getcontent("http://www.kuaidi100.com/");
        //爬取快递名称对应的快递代码
        preg_match_all("/data-code="(?P<name_code>w+)">(?P<name>.*)</a>/iU",$result,$data);//(?P<name>)正则匹配存储字符
        $name = array();
        //var_dump($data['name']);
        //echo "<br>";
        foreach($data['name'] as $k=>$v){
            $name[$v] =$data['name_code'][$k];//例:$name[中通]=zhontong
            //echo "[".$v."]=".$name[$v]."<br>";//获取快递编码
        }    
        //echo "一次查询"."<br>";    
        return $name;
    }
    
    /*
     * 解析object成数组的方法
     * @param $json 输入的object数组
     * return $data 数组
     */
    private function json_array($json){
        if($json){
            foreach ((array)$json as $k=>$v){
                $data[$k] = !is_string($v)?$this->json_array($v):$v;
            }
            return $data;
        }
    }
    public  function getorder($name,$order){
        $keywords = $this->expressname[$name];
        //var_dump($keywords);
        //$keywords=shunfeng;
        //$order=688625443909;
        $result = $this->getcontent("http://www.kuaidi100.com/query?type={$keywords}&postid={$order}");
        $result = json_decode($result);
        $data = $this->json_array($result);
        return $data;
    }
}
$a = new Express();

echo "PHP快速抓取快递信息"."<br>";

echo "<p>/************************************************************/</br>";
for($num=688625443900;$num<=688625443910;$num++)
{
    echo "<br>快递单号:".$num;
    $result = $a->getorder("顺丰",$num);
    if(isset($result['nu']))
    {
      echo "    更新时间".$result['updatetime']."<br>";//生成表格
      echo "<table border="1">";
      echo "<tr>";
      echo "<th>时间</th>";
      echo "<th>地点和跟踪进度</th>";
      echo "</tr>";
      foreach($result['data'] as $k=>$v)
      {
          echo "<tr>";
          echo "<td>".$v['time']."</td>";
          echo "<td>".$v['context']."</td>";
          echo "</tr>";
      }
      echo "</table>";
    }
    else
    {
        echo "<br>".$result['message'];//输出错误信息
        //var_dump($result);
    }
}
echo "</br><p>/************************************************************/</p>";
?>
原文地址:https://www.cnblogs.com/zeze/p/4915944.html