数组拼接成字符串请求url

/**
* url_merge 拼接http url请求地址和参数
* @param1 $query_data array() 要拼接参数的键名->值
* @return string 拼接完成要请求的url地址 字符串
*/
private function url_merge($query_data)
{
$str = 'http://122.152.175.3/webapi/interface.aspx?'; //API url
$res = '';
$count = count($query_data);
$i = 0;
foreach ($query_data as $k => $v) {
if ($i < $count - 1) {
$res .= $k . '=' . $v . '&';
} else {
$res .= $k . '=' . $v;
}
$i++;
}
return $str.$res;
}

    // 游戏平台创建游戏账号(action=create)
    public function create()
    {
        if(IS_POST){
            //接收表单提交的数据
            $userinfo = I('request.');
            //获取用户名
            $name = $userinfo['username'];
            //组装验证码
            $verifyCode = strtoupper(md5('QX'.'COMA'.$name.'create'.'TAG'.'QX'.'qluxuOK4j8OdFs78pmd'));
            $arr = array('secret'=>'QX','agent'=>'COMA','username'=>$name,'action'=>'create','currency'=>'TAG','channel'=>'QX','verifyCode'=>$verifyCode);
            $url = $this -> url_merge($arr);
            //发送请求
            $result = self::curl($url);
            //返回的xml数据转换成 xml 对象
            $xml = simplexml_load_string ($result);

            if( $xml->errcode == 0){
                //状态吗为零 ->注册成功 写入用户数据库
                $pk = D('User')->add(array('username'=>$userinfo['username'],'password'=>md5($userinfo['password'])));
                if( $pk > 0 ){
                    echo "创建成功了";
                    return $xml -> errcode;
                }
            }else{
                echo $xml->errtext;
                dump(json_decode(json_encode($xml),true));
            }
        }
    }

你可能感兴趣的:(数组拼接成字符串请求url)