小白求助:关于PHP的SDK包怎么用?
发布于 6 年前 作者 cuijie 1219 次浏览 来自 官方Issues

草鸡小白,不要见笑~

就是怎么把从数据库中的这数据传给过去,和接收返回的数据值这些

比如foreach的数据是以下这个表,我怎么把$row[]; 的值传过去。。。。

查询后输出:

  <?php
  foreach( $rows as $row ){
    ?>
      <tr>
        <th scope="col"><?php echo $row ['aaa'];?></th>
        <th scope="col"><?php echo $row ['bbb'];?></th>
        <th scope="col"><?php echo $row ['ccc'];?></th>
        <th scope="col"><?php echo $row ['...'];?></th>
      </tr>
   <?php
   }
   ?>


SDK包:
<?php

//调用下单
(new demo())->order();

/**
 * Class demo
 * demo 开放平台下单
 */
class demo
{
    /**
     * [@var](/user/var) string 接口地址 测试地址
     */
	static $serverUrl = "http://Urlbalabalabalabala";
    /**
     * [@var](/user/var) string 创建应用分配的appkey
     */
	static $appKey = "999999";
    /**
     * [@var](/user/var) string 创建应用分配的appsecret
     */
	static $appSecret = "wocaowozhendechaojilihaihahahaha";
    /**
     * 下单
     */
	public static function order(){
	    //下单报文
		$info = [
			'appid'=>"999999",
			'backparam'=>"测试",
			'backurl'=>"http://www",
			'freight'=>10,
			'items'=>[
				[
					'name'=>'衣服',
					'number'=>1,
					'remark'=>'袜子',
				]
			],
			'orderid'=>"2333098766",
			'other_charges'=>0,
			'premium'=>1,
			'receiver'=>[
				'address'=>'青浦区盈港东路 6679 号',
				'city'=>'上海市',
				'mobile'=>'021-99999999',
				'name'=>'李四',
				'phone'=>'13888888888',
				'county'=>'青浦区',
				'company'=>'string',
				'postcode'=>'string',
				'province'=>'上海市',
			],
			'remark'=>"string",
			'sendendtime'=>"2019-09-03 11:00:00",
			'sender'=>[
				'address'=>'青浦区盈港东路 6679 号',
				'city'=>'上海市',
				'mobile'=>'021-99999999',
				'name'=>'李四',
				'phone'=>'13888888888',
				'county'=>'青浦区',
				'company'=>'string',
				'postcode'=>'string',
				'province'=>'上海市',
			],		
			'sendstarttime'=>"2021-05-03 10:00:00",
			'size'=>"0.12,0.23,0.11",
			'special'=>0,
			'value'=>126.5,
			'weight'=>0,
		];
        //请求参数body,指定JSON格式   转json
		$json_info = json_encode($info,JSON_UNESCAPED_UNICODE);
		//生成的SIGN签名串
		$sign = md5($json_info.'_'.self::$appSecret);
		//header
		$header = [
			'app-key:'.self::$appKey,
			'sign:'.$sign,
			'req-time:'.time(),
			'Content-Type:application/json;charset=UTF-8',
		];
		//http请求
        try {
            $res = (new Http())->postJson(self::$serverUrl, $json_info, $header);
            if (!empty($res)){
                $res_array = json_decode($res,true);
                if ($res_array['code'] === '0000'){
                    echo '请求成功'.PHP_EOL;
                }else{
                    echo '请求失败'.self::getResCodeInfo($res_array['code']).PHP_EOL;
                }
                echo $res.PHP_EOL;
            }else{
                echo '请求失败:无返回';
            }
        }catch (Exception $e){
            echo '请求失败:'.$e->getMessage();
        }
	}

    /**
     * 返回报文code对应关系
     * [@param](/user/param) $code
     * [@return](/user/return) string
     */
	private static function getResCodeInfo($code){
	    switch ($code){
            case '0000':
                return "请求成功";
            case '7100':
                return "账号无权限";
            case '7200':
                return "接口无权限";
            case '7300':
                return "IP无权限";
            case '7400':
                return "签名失败";
            case '7500':
                return "超过单用户日最高访问量";
            case '7501':
                return "超过日访问量最高值";
            case '7502':
                return "超过单用户接口QPS最大限制";
            case '7503':
                return "超过该接口QPS最大限制";
            case '7600':
                return "头信息header参数中缺少app-key";
            case '7601':
                return "头信息header参数中缺少sign";
            case '7602':
                return "头信息header参数中缺少req-time";
            case '7603':
                return "content-type只支持application/json;utf-8格式";
            case '7604':
                return "httpmothod只支持post类型";
            case '7605':
                return "请求body参数不能为空";
            case '7777':
                return "内部服务错误";
            default:
                return '未知错误';
        }
    }
}

/**
 * http
 * Class Http
 */
class Http
{
    /**
     * http 请求
     * [@param](/user/param) $url
     * [@param](/user/param) $data
     * [@param](/user/param) array $header
     * [@param](/user/param) int $timeout
     * [@return](/user/return) mixed|string
     * [@throws](/user/throws) Exception
     */
    public static function postJson($url, $data, $header = array(
        "Content-Type: application/json;charset=UTF-8"
    ),$timeout=5)
    {
        $url = trim($url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $res = curl_exec($ch);
        if ($res === false) {
        	$res = curl_error($ch);
        	throw new Exception($res, 1);
        }
        curl_close($ch);
        return $res;
    }
}

1 回复

先报个班把基础学好吧

回到顶部