php demo文件(快速对接)
<pre><code>&lt;?php
class Demo {
private $appKey = &quot;x&quot;;
private $appSecret = &quot;y&quot;;
private $shopId = 1;
/**
此处调用最简单的`查询余额接口`,这个方法如有正常返回数据,则表示签名通过了.
*/
function queryBalance() {
// 系统参数(固定参数)
$sysParams = [
&quot;appkey&quot; =&gt; $this-&gt;appKey,
&quot;timestamp&quot; =&gt; time(),
&quot;version&quot; =&gt; &quot;1.0&quot;,
];
// 接口参数
$apiParams = [
&quot;sss_shop_id&quot; =&gt; $this-&gt;shopId,
];
// 获取签名
$sign = $this-&gt;makeSign($apiParams, $sysParams[&quot;timestamp&quot;], $this-&gt;appSecret);
// 组装最终的请求参数
$params = array_merge($sysParams, $apiParams);
$params[&quot;sign&quot;] = $sign;
// 请求开放平台接口
$result = json_decode(
$this-&gt;httpPost(&quot;https://peisongopen.51sssong.com/api/v1/shop/queryBalance&quot;, $params),
true
);
if(isset($result[&quot;code&quot;]) &amp;&amp; $result[&quot;code&quot;] == 0) { // code:0表示接口响应正常并且成功
var_dump( $result );
} else { // 接口异常
var_dump( $result );
}
exit;
}
/**
* 签名
* @param array $param
* @param int $time
* @return string
*/
function makeSign($param, $time, $appSecret = &quot;&quot;)
{
$version = &quot;1.0&quot;;
$tmpArr = array(
&quot;appkey&quot; =&gt; $this-&gt;appKey,
&quot;timestamp&quot; =&gt; $time,
&quot;version&quot; =&gt; $version,
);
foreach ($param as $k =&gt; $v)
$tmpArr[$k] = $v;
ksort($tmpArr);
if($appSecret) {
$str = $appSecret;
}else {
$str = $appSecret;
}
foreach ($tmpArr as $k =&gt; $v) {
if ($v === false)
$v = 'false';
if ($v === true)
$v = 'true';
if (empty($v) &amp;&amp; $v != 0)
continue;
$str .= $k . $v;
}
$signature = sha1($str);
return strtolower($signature);
}
/**
* POST 请求
* @param string $url
* @param array $param
* @param boolean $post_file 是否文件上传
* @return string content
*/
function httpPost($url, $param, $post_file = false)
{
$oCurl = curl_init();
if (stripos($url, &quot;https://&quot;) !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = [];
foreach ($param as $key =&gt; $val) {
$aPOST[] = $key . &quot;=&quot; . urlencode($val);
}
$strPOST = join(&quot;&amp;&quot;, $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_POST, true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
curl_setopt($oCurl, CURLOPT_TIMEOUT, 10);
$header = array(
'application/x-www-form-urlencoded',
);
curl_setopt($oCurl, CURLOPT_HTTPHEADER, $header);
$sContent = curl_exec($oCurl);
curl_close($oCurl);
return $sContent;
}
}
$result = (new Demo()) -&gt; queryBalance();
var_dump($result);</code></pre>