<?php
class CainiaoHelper{
//public $debug=false;
public $debug=true;
function __construct(){
}
public function debug($msg) {
if ($this->debug) {
print '<div style="border: 1px solid red; padding: 0.5em; margin: 0.5em;">';
echo "<pre>";
print_r($msg);
echo "</pre>";
print '</div>';
}
}
private function curl_get_contents($url,$data = array(), $https = false)
{
$results['error'] = '';
$results['status'] = 0;
$results['data'] = array();
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$curl = curl_init(); // 启动一个CURL会话
if( !empty($data) && is_array($data) )
{
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
//curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
}
if($https )
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
}
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
curl_setopt($curl, CURLOPT_USERAGENT,$user_agent); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
$results['data']= curl_exec($curl); // 执行操作
if (curl_errno($curl))
{
$results['error'] = curl_error($curl); //捕抓异常
}
curl_close($curl); // 关闭CURL会话
return $results['data']; // 返回数据
}
public function curl_http_post($url, $data)
{
return $this->curl_get_contents($url,$data);
}
public function curl_http_get($url)
{
return $this->curl_get_contents($url,array());
}
public function curl_https_post($url, $data)
{
return $this->curl_get_contents($url,$data,true);
}
public function curl_https_get($url)
{
return $this->curl_get_contents($url,array(),true);
}
}
class Cainiao extends CainiaoHelper
{
public $Urlgateway = "http://link.cainiao.com/gateway/link.do";
public $AppKey;
public $SecretKey;
public $Token;
public $debug=false;
public $ishttps=false;
function __construct(){
parent::__construct();
}
/**
订单渠道平台编码:淘宝(TB)、天猫(TM)、京东(JD)、当当(DD)、
拍拍(PP)、易讯(YX)、ebay(EBAY)、QQ网购(QQ) 、亚马逊(AMAZON)、
苏宁(SN)、国美(GM)、唯品会(WPH)、聚美(JM)、乐蜂(LF)、蘑菇街(MGJ) 、
聚尚(JS)、拍鞋(PX)、银泰(YT)、1号店(YHD)、
凡客(VANCL)、邮乐(YL)、优购(YG)、阿里 巴巴(1688)、其他(OTHERS)
*/
private function getDigest($apiContent){
$combine = md5($apiContent.$this->SecretKey,true);
return base64_encode($combine);
}
public function getCpCode(){
$A=array(
'YTO'=>'圆通',
'HTKY'=>'百世快递',
'YUNDA'=>'韵达',
'GTO'=>'国通',
'DBKD'=>'德邦快递 ',
'EMS'=>'EMS',
'POSTB'=>'邮政快递包裹',
'STO'=>'申通',
'SF'=>'顺丰',
'ZTO'=>'中通 ',
'ZJS'=>'宅急送',
);
return $A;
}
/**
* 获取发货地,CP开通状态,账户的使用情况 Version:v1.0
* Enter description here ...
*/
public function Tms_Waybill_Subscription_Query($input='YTO'){
if(!$input) return false;
$A['logistics_interface']='{"cpCode":"'.$input.'"}';
$A[data_digest]=base64_encode(md5($A[logistics_interface].$this->SecretKey,true));
$A[msg_type]='TMS_WAYBILL_SUBSCRIPTION_QUERY';
$A[logistic_provider_id]=$this->Token;
if($this->ishttps){
return $s=$this->curl_https_post($this->Urlgateway,$A);
}else{
return $s=$this->curl_http_post($this->Urlgateway,$A);
}
}
public function GET_CLOUDPRINT_STANDARD_TEMPLATES($input='YTO'){
if(!$input) return false;
$A['logistics_interface']='{"cpCode":"'.$input.'"}';
$A[data_digest]=base64_encode(md5($A[logistics_interface].$this->SecretKey,true));
$A[msg_type]='CLOUDPRINT_STANDARD_TEMPLATES';
$A[logistic_provider_id]=$this->Token;
if($this->ishttps){
return $s=$this->curl_https_post($this->Urlgateway,$A);
}else{
return $s=$this->curl_http_post($this->Urlgateway,$A);
}
}
/**
* 电子面单云打印取号接口 Version:v1.0
* Enter description here ...
* @param unknown_type $input
*/
public function Tms_Waybill_Get($input='')
{
if(!$input) return false;
$A[logistics_interface]=$input;
$A[data_digest]=base64_encode(md5($A[logistics_interface].$this->SecretKey,true));
$A[msg_type]='TMS_WAYBILL_GET';
$A[logistic_provider_id]=$this->Token;
if($this->ishttps){
return $s=$this->curl_https_post($this->Urlgateway,$A);
}else{
return $s=$this->curl_http_post($this->Urlgateway,$A);
}
}
/**
* 电子面单云打印更新接口 Version:v1.0
* Enter description here ...
* @param unknown_type $input
*/
public function Tms_Waybill_Update($input='')
{
if(!$input) return false;
$A[logistics_interface]=$input;
$A[data_digest]=base64_encode(md5($A[logistics_interface].$this->SecretKey,true));
$A[msg_type]='TMS_WAYBILL_UPDATE';
$A[logistic_provider_id]=$this->Token;
if($this->ishttps){
return $s=$this->curl_https_post($this->Urlgateway,$A);
}else{
return $s=$this->curl_http_post($this->Urlgateway,$A);
}
}
/**
* ISV电子面单取消接口 Version:v1.0
* Enter description here ...
* @param unknown_type $input
*/
public function Tms_Waybill_Discard($input='')
{
if(!$input) return false;
$A[logistics_interface]=$input;
$A[data_digest]=base64_encode(md5($A[logistics_interface].$this->SecretKey,true));
$A[msg_type]='TMS_WAYBILL_DISCARD';
$A[logistic_provider_id]=$this->Token;
if($this->ishttps){
return $s=$this->curl_https_post($this->Urlgateway,$A);
}else{
return $s=$this->curl_http_post($this->Urlgateway,$A);
}
}
}