快递100接口开发

1.预约

function kuaidi100Yuyue($company,$number){
    $post_data = array();
    $post_data["schema"] = 'json' ;
//callbackurl请参考callback.php实现,key经常会变,请与快递100联系获取最新key


    $json_data=array(
            'company'=>$company,
            'number'=>$number,
            'key'=>'key',
            'parameters'=>array(
                    'callbackurl'=>'http://www.bellevplus.com/callback/'
            )
    );
    $post_data["param"] = json_encode($json_data);

    $url='http://www.kuaidi100.com/poll';

    $o="";
    foreach ($post_data as $k=>$v)
    {
        $o.= "$k=".urlencode($v)."&";       //默认UTF-8编码格式
    }

    $post_data=substr($o,0,-1);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $result = curl_exec($ch);       //返回提交结果,格式与指定的格式一致(result=true代表成功)
    curl_close($ch);
    $res=json_decode($result,true);
    return $res;
}

2.接受数据

header("Content-Type:text/html;charset=utf-8");
$param=isset($_POST['param']) && !empty($_POST['param'])?$_POST['param']:'';


if($param!=''){
    $res=json_decode(stripslashes($param),true);
    $odd_num=$res['lastResult']['nu'];
    if(insetLoistics($param,$odd_num)){
        //$param包含了文档指定的信息,...这里保存您的快递信息,$param的格式与订阅时指定的格式一致
        echo  '{"result":"true","returnCode":"200","message":"成功"}';
        //要返回成功(格式与订阅时指定的格式一致),不返回成功就代表失败,没有这个30分钟以后会重推
    }else{
        echo  '{"result":"false","returnCode":"500","message":"失败"}';
        //保存失败,返回失败信息,30分钟以后会重推
    }
}
function insetLoistics($content,$odd_num){
    global $wpdb;
    $data['logistics_content']=$content;
    $data['create_time']=date("Y-m-d H:i:s",time());
    $table=$wpdb->prefix.'order_logistics';
    $res=$wpdb->query("UPDATE $table set logistics_content='$content' WHERE odd_num='$odd_num'");
    if($res){
        return true;
    }else{
        return false;
    }
}

 

posted @ 2016-09-03 18:02  myvic  阅读(1995)  评论(0编辑  收藏  举报