设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 157|回复: 1

php快递查询API类-支持各种快递

[复制链接]
发表于 2014-1-3 09:39:49 | 显示全部楼层 |阅读模式
php快递查询API类-支持各种快递单独拿出来封装成一个小类,然后大家需要的时候用吧!
  1. <?php
  2. /**
  3. * Express.class.php 快递查询类
  4. *
  5. * @copyright widuu
  6. * @license http://www.widuu.com
  7. * @lastmodify 2013-6-19
  8. */

  9. class Express {

  10. private $expressname =array(); //封装了快递名称

  11. function __construct(){
  12. $this->expressname = $this->expressname();
  13. }

  14. /*
  15. * 采集网页内容的方法
  16. */
  17. private function getcontent($url){
  18. if(function_exists("file_get_contents")){
  19. $file_contents = file_get_contents($url);
  20. }else{
  21. $ch = curl_init();
  22. $timeout = 5;
  23. curl_setopt($ch, CURLOPT_URL, $url);
  24. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  25. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  26. $file_contents = curl_exec($ch);
  27. curl_close($ch);
  28. }
  29. return $file_contents;
  30. }
  31. /*
  32. * 获取对应名称和对应传值的方法
  33. */
  34. private function expressname(){
  35. $result = $this->getcontent("http://www.kuaidi100.com/");
  36. preg_match_all("/data\-code\="(?P<name>\w+)"\>\<span\>(?P<title>.*)\<\/span>/iU",$result,$data);
  37. $name = array();
  38. foreach($data['title'] as $k=>$v){
  39. $name[$v] =$data['name'][$k];
  40. }
  41. return $name;
  42. }

  43. /*
  44. * 解析object成数组的方法
  45. * @param $json 输入的object数组
  46. * return $data 数组
  47. */
  48. private function json_array($json){
  49. if($json){
  50. foreach ((array)$json as $k=>$v){
  51. $data[$k] = !is_string($v)?$this->json_array($v):$v;
  52. }
  53. return $data;
  54. }
  55. }

  56. /*
  57. * 返回$data array 快递数组
  58. * @param $name 快递名称
  59. * 支持输入的快递名称如下
  60. * (申通-EMS-顺丰-圆通-中通-如风达-韵达-天天-汇通-全峰-德邦-宅急送-安信达-包裹平邮-邦送物流
  61. * DHL快递-大田物流-德邦物流-EMS国内-EMS国际-E邮宝-凡客配送-国通快递-挂号信-共速达-国际小包
  62. * 汇通快递-华宇物流-汇强快递-佳吉快运-佳怡物流-加拿大邮政-快捷速递-龙邦速递-联邦快递-联昊通
  63. * 能达速递-如风达-瑞典邮政-全一快递-全峰快递-全日通-申通快递-顺丰快递-速尔快递-TNT快递-天天快递
  64. * 天地华宇-UPS快递-新邦物流-新蛋物流-香港邮政-圆通快递-韵达快递-邮政包裹-优速快递-中通快递)
  65. * 中铁快运-宅急送-中邮物流
  66. * @param $order 快递的单号
  67. * $data['ischeck'] ==1 已经签收
  68. * $data['data'] 快递实时查询的状态 array
  69. */
  70. public function getorder($name,$order){
  71. $keywords = $this->expressname[$name];
  72. $result = $this->getcontent("http://www.kuaidi100.com/query?type={$keywords}&postid={$order}");
  73. $result = json_decode($result);
  74. $data = $this->json_array($result);
  75. return $data;
  76. }
  77. }
  78. $a = new Express();
  79. $result = $a->getorder("全一快递",111309582915);
  80. var_dump($result);
  81. ?>
复制代码

 楼主| 发表于 2014-1-3 09:42:55 | 显示全部楼层
$result = $a->getorder("全一快递",111309582915);
建议最后个参数不要用数字形式传递,使用字符串形式
$result = $a->getorder("全一快递","111309582915");
有的快递号已经到13位,会被php转换
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-12-12 22:32 , Processed in 0.015464 second(s), 16 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表