The request is badly formed

原因:url中包含空格
背景:做项目对接时,用的是get请求,然后输入的参数中有空格就会报这个错误
解决方法:
1,url中包含空格,使用urlencode去掉空格。这是在网上找的,但是不适合我的

*****************************************************************
//GBK
//echo urlencode("中文-_. ")."\n"; //%D6%D0%CE%C4-_.+
//echo urldecode("%D6%D0%CE%C4-_. ")."\n"; //中文-_.
//echo rawurlencode("中文-_. ")."\n"; //%D6%D0%CE%C4-_.%20
//echo rawurldecode("%D6%D0%CE%C4-_. ")."\n"; //中文-_.
//
 
//UTF-8
//echo urlencode("中文-_. ")."\n"; //%E4%B8%AD%E6%96%87-_.+
//echo urldecode("%E4%B8%AD%E6%96%87-_. %E4%B8%AD%E6%96%87")."\n";//中文-_. 中文
//echo rawurlencode("中文-_. ")."\n";//%E4%B8%AD%E6%96%87-_.%20
//echo rawurldecode("%E4%B8%AD%E6%96%87-_.%20%E4%B8%AD%E6%96%87-_.%20")."\n"; //中文-_. 中文-_.

2,使用get请求时,将所有的值放在数组中,而不是拼接url
我的get请求是把接受的参数拼接,参数值有空格时,就会报错,因此使用了另一种方法

这是拼接url,
 $url = "https://baidu.com/api/mami/NursingHandler.ashx?act=GETBABYRECORDINFONEW&babyId=".$data['babyid']."&custId=".$custId."&Code=".$data['code'];
 $list =  CurlHelper::getInstance()->curl_get_https($url);
 curl_get_https方法:
     function curl_get_https($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  // 跳过检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 跳过检查
        $tmpInfo = curl_exec($curl); 
        curl_close($curl);
        return $tmpInfo;   //返回json对象
    }
 public function actionEvaluate(){
        $post = \Yii::$app->request->post();
        $custId = CurlHelper::getInstance()->getErpHsId(\Yii::$app->mall->id);
        $user_id =CurlHelper::getInstance()->getErpUserId(\Yii::$app->user->id);
        $data['act'] ='EVALUATE';
        $data['custId'] =$custId;
        $data['userid'] =$user_id;
        $data['marketType'] = $post['markType'];
        $data['projectid'] = $post['projectid'];
        $data['content'] = $post['content'];
        $data['star'] = $post['star'];
        
        $gen ="https://mm.hxqt.cn/api/mami/CustomerHandler.ashx";
        $url = $gen . "?" . http_build_query($data);
        $list =  CurlHelper::getInstance()->httpGet($url);
        return [
            'code' => ApiCode::CODE_SUCCESS,
            'data' =>[
                'list'=>$list,
            ]
        ]; 
    }  
httpGet的方法:
 public function httpGet($url, $param = array(), $headers = array())
    {
        try {
            $url = $this->appendParams($url, $param);
            $client = $this->getClient($headers);
            $response = $client->get($url);
            $body = $response->getBody();
        } catch (ClientException $e) {
            $body = $e->getResponse()->getBody();
        }
        if (!$body) {
            throw new \Exception('请求没有有效的返回');
        }
        $res = json_decode($body->getContents(), true);
        if (!$res) {
            throw new \Exception('请求返回值为空');
        }
        return $res;
    }
private function appendParams($url, $params = [])
    {
        if (!is_array($params)) {
            return $url;
        }
        if (!count($params)) {
            return $url;
        }
        $url = trim($url, '?');
        $url = trim($url, '&');
        $queryString = $this->paramsToQueryString($params);
        if (mb_stripos($url, '?')) {
            return $url . '&' . $queryString;
        } else {
            return $url . '?' . $queryString;
        }
    }
        public function getClient($headers = array())
    {
        return new Client([
            'verify' => false,
            'headers' => $headers
        ]);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Most programming languages contain good and bad parts, but JavaScript has more than its share of the bad, having been developed and released in a hurry before it could be refined. This authoritative book offers a detailed explanation of the features that make JavaScript an outstanding object-oriented programming language, and warns you about the bad parts., In the process, JavaScript: The Good Parts defines a subset of JavaScript that's more reliable, readable, and maintainable than the language as a whole. Author Douglas Crockford, a member of JavaScript 2.0 committee at ECMA, is considered by many people in the development community to be the JavaScript expert., A beautiful, elegant, lightweight and highly expressive language lies buried under a steaming pile of good intentions and blunders, he explains. The very good ideas include functions, loose typing, dynamic objects, and an expressive object literal notation. Awful ideas include a programming model based on global variables. With JavaScript: The Good Parts, you can release this elegant programming language from its old shell, and create more maintainable, extensible, and efficient code., The book's topics include:, * Syntax, * Objects, * Functions, * Inheritance, * Arrays, * Regular expressions, * Methods, * Style, * Beautiful features, Appendices summarize JavaScript's bad parts and awful parts. But the greatest benefit of studying the good parts is that you can avoid the need to unlearn the bad parts. If you want to learn more about the bad parts and how to use them badly, consult any other JavaScript book., JavaScript is the language of the Web -- the only language found in all browsers -- so avoiding it altogether is not an alternative. But, whether you're managing object libraries or just trying to get Ajax to run fast, Crockford's guidance in JavaScript: The Good Parts will help you create truly effective JavaScript code.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值