上篇播客,介绍了容联云通信短信接口的应用,并且封装过cURL的函数 , 在这里介绍一下快递100接口的简单应用,再次封装一个cURL发送方法写出:如果不想用这个函数,也可以使用file_get_content() 方法效果一致。在这里,我们来使用cURL方法来实现
//getf方式的请求
function getRequest($url){
// var_dump($url);die;
$ch = curl_init();
//设置请求的路径
curl_setopt($ch,CURLOPT_URL,$url);
//不需要验证ssl证书
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
//设置获取的信息以文件流的形式返回,不在页面中输出任何结果
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$str = curl_exec($ch);
curl_close($ch);
return $str;
}
然后直接就可以用快递100提供好的接口的网址
type参数:是要查询的快递公司名字;
postid:是要查询的快递单号
当然也有很多其他的参数,可以去官网自己查看。。这里只做简单的操作
(http://www.kuaidi100.com/query?type=jd&postid=23452342352)
先创建一个简单的模板 view:
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<style type="text/css" >
form{
margin:200px auto;
width:500px;
}
</style>
<body>
<form action="" method="post">
<table>
<tr>
<td>快递单号:</td>
<td><input type="text" name="id" id="idcard" ></td>
</tr>
<tr>
<td>快递公司:</td>
<td><input type="text" name="type" id="company" ></td>
</tr>
<tr>
<td colspan="2"><input type="button" id='btn' value="查询快递"/></td>
</tr>
<tr>
<td colspan="2"><div id="html"></div></td>
</tr>
</table>
</form>
</body>
</html>
<js href="__JQUERY__/jquery-1.6.2.min.js" />
<script>
$('#btn').click(function(){
//获取快递单号
var idcard = $('#idcard').val();
var company = $('#company').val();
//连接短信ajax
$.get("__CONTROLLER__/kuaidi/type/"+company+"/id/"+idcard,function(data){
//var obj = $.parseJSON(data);
//如果返回的sign的0,表示短信发送成功!
$('#html').html(data);
});
})
</script>
效果图如下:
php 的 controller 如下:
/**
*
* @param string $type 快递公司 yuantong
* @param string $id 快递单号 500422249968
*/
public function kuaidi($type=0 , $id=0){
if(IS_AJAX){
//快递100的查询快递的接口,
$url="http://www.kuaidi100.com/query?type=$type&postid=$id";
//调用以上方法去获取数据
$str = getRequest($url);
//返回的是json格式的字符串,转化为数组
$arr = json_decode($str ,true);
if($arr['message']=='ok'){
foreach($arr['data'] as $v){
echo '您的宝贝:<br/>时间:'.$v['time'].'<br/>状态:'.$v['context'];
echo '<hr/>';
}
}else{
echo '亲!查不到你的宝贝哦!<hr/>';
}
die;
}
$this->display();
}
让后你就可以直接使用快递100了
效果图: