21,891
社区成员
发帖
与我相关
我的任务
分享$s =file_get_contents('http://www.kuaidi100.com/query?type=yuantong&postid=11111111111');
$data = json_decode($s,1);
echo 'state:' . $data['state'] . '<br/>';
echo 'lastmsg:'. $data['data'][0]['time'] . ','. $data['data'][0]['context'];


$s =file_get_contents('http://www.kuaidi100.com/query?type=yuantong&postid=11111111111');
$data = json_decode($s,1);
前端用ajax调用你这个php,这样接口慢不会影响你前端页面的加载。
第二种方法是使用iframe嵌套
<iframe frameborder=0 width=170 height=100 marginheight=0 marginwidth=0 scrolling=no src=‘http://www.kuaidi100.com/query?type=yuantong&postid=11111111111’></iframe>

<?php
function getMicrotime(){
list($usec, $sec) = explode(' ', microtime());
return (float)$usec + (float)$sec;
}
$start = getMicrotime();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.kuaidi100.com/query?type=yuantong&postid=11111111111");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // 3秒超时
curl_setopt($ch, CURLOPT_HEADER, 0);
$s = curl_exec($ch);
curl_close($ch);
//$s =file_get_contents('http://www.kuaidi100.com/query?type=yuantong&postid=11111111111');
$end = getMicrotime();
echo 'run time:'.(($end-$start)*1000).'ms<br/>';
$data = json_decode($s,1);
echo 'state:' . $data['state'] . '<br/>';
echo 'lastmsg:'. $data['data'][0]['time'] . ','. $data['data'][0]['context'];
function getMicrotime(){
list($usec, $sec) = explode(' ', microtime());
return (float)$usec + (float)$sec;
}
$start = getMicrotime();
$s =file_get_contents('http://www.kuaidi100.com/query?type=yuantong&postid=11111111111');
$end = getMicrotime();
echo 'run time:'.(($end-$start)*1000).'ms<br/>';
$data = json_decode($s,1);
echo 'state:' . $data['state'] . '<br/>';
echo 'lastmsg:'. $data['data'][0]['time'] . ','. $data['data'][0]['context'];
我测试访问,执行时间90多ms,不算太慢了。
run time:90.999126434326ms
state:5
lastmsg:2017-07-24 21:17:01,快件已到达派送中妈妈驿站,如有疑问请联系63762999
如果你那边觉得慢,可以使用命令traceroute www.kuaidi100.com,看看那个网络节点卡住了。
<?php
if(isset($_GET['c']) && isset($_GET['no'])){
$s =file_get_contents('http://www.kuaidi100.com/query?type=' . $_GET['c'] . '&postid=' . $_GET['no']);
echo $s;
exit;
}
?>
<form method="post" action="">
快递公司<input type="text" id="type" value="yuantong"/> 运单号码<input type="text" id="no" value="11111111111" /> <input type="button" value="查询" id="refresh">
</form>
<span id="res"></span>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$('#refresh').click(function(){
$.getJSON('?c=' + $('#type').val() + '&no=' + $('#no').val()+'&'+Math.random(),function(d){
$('#res').html('state:' + d['state'] + '<br/>lastmsg:' +d['data'][0]['time'] + ','+ d['data'][0]['context']);
})
})
</script>