php调用淘宝接口

<?php

$para = array(
    'format' => 'json',
    'v' => '2.0',
    'sign_method' => 'md5',
    'app_key' => $app_key,
    'app_secret' => $app_secret,
    'session' => $app_session,
    'app_nick' => $app_nick,
    'app_type' => 'B',
    'method' => 'taobao.trade.get',
    'fields' => 'tid,type,status,payment,orders',
    'tid'   =>$tid
);
$result = do_execute($url, $para, $para);
//var_dump($result);exit;
$arr = json_decode($result);
print_r($arr);exit;

function do_execute( $url, $apiParams, $sign_conf) {
    $timestamp = date("Y-m-d H:i:s");
    $apiParams ['timestamp'] = $timestamp;
    $sign_conf['timestamp'] =   $timestamp;
    $apiParams ['sign'] = createSign ( $apiParams['app_secret'], $sign_conf); // 签名生成sign

    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_FAILONERROR, false );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt ( $ch, CURLOPT_TIMEOUT, 30 );

    // 如果参数为数组则
    if (is_array ( $apiParams ) && 0 < count ( $apiParams )) {
        $postBodyString = "";
        foreach ( $apiParams as $k => $v ) {
            $postBodyString .= "$k=" . urlencode ( $v ) . "&";
        }
        unset ( $k, $v );
    } else {
        $postBodyString = $apiParams;
    }

    try {
        curl_setopt ( $ch, CURLOPT_POST, true );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, substr ( $postBodyString, 0, - 1 ) );
        $reponse = curl_exec ( $ch );
        if (curl_errno ( $ch )) {
            $curl_error = curl_error ( $ch );
            throw new Exception ( $curl_error, 0 );
        } else {
            $httpStatusCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
            if (200 !== $httpStatusCode) {
                throw new Exception ( $reponse, $httpStatusCode );
            }
        }
    } catch (Exception $e) {}

    curl_close ( $ch );
    return $reponse;
}
/**
 * 生成签名
 */
function createSign($appSecret, $paramArr) {
    $sign = $appSecret;
    ksort ( $paramArr );
    foreach ( $paramArr as $key => $val ) {
        if ($key != '' && $val != '') {
            $sign .= $key . $val;
        }
    }

    $sign .= $appSecret;
    $sign = strtoupper ( md5 ( $sign ) );
    return $sign;
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值