php之curl带头部header的post请求

    public  function op(){
          // $url = 'http://www.baidu.com/test/na/oh';
   
                $header=array('Accept-Language:zh-CN','x-appkey:114816004000028','x-apsignature:933931F9124593865313864503D477035C0F6A0C551804320036A2A1C5DF38297C9A4D30BB1714EC53214BD92112FB31B4A6FAB466EEF245710CC83D840D410A7592D262B09D0A5D0FE3A2295A81F32D4C75EBD65FA846004A42248B096EDE2FEE84EDEBEBEC321C237D99483AB51235FCB900AD501C07A9CAD2F415C36DED82','x-apversion:1.0','Content-Type:application/x-www-form-urlencoded','charset:utf-8','Accept:application/json','X-APFormat:json');
     
                 $content='params=[{"v":"3.0","pwd":"96e79218965eb72c92a549dd5a330112","action":"msc/user/login","merchId":"114816004000028","iposSn":"0000000000000000","operator":"01"}]';
            //   $content = array(
              //      'name' => 'fdipzone'
             //   );
              $response =$this->tocurl($url, $header,$content);
             var_dump($response);
           
         }

        /**
         * 发送数据
         * @param String $url     请求的地址
         * @param Array  $header  自定义的header数据
         * @param Array  $content POST的数据
         * @return String
         */
        public  function tocurl($url, $header,$content){
            $ch = curl_init();
            if(substr($url,0,5)=='https'){
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
            }
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
            $response = curl_exec($ch);
            if($error=curl_error($ch)){
                die($error);
            }
            curl_close($ch);
          //var_dump($response);
            return $response;
        }
  
  
  
  
  
  
  
          public  function oh(){
                    $post_data = $_POST;
                        var_dump();
                    $header = $this->em_getallheaders();
                     var_dump($header);exit;
                    $ret = array();
                    $ret['post'] = $post_data;
                    $ret['header'] = $header;
                   
                    header('content-type:application/json;charset=utf8');
                    echo  json_encode($ret, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
            }
          /**
           * 获取自定义的header数据
           */
          public   function get_all_headers(){
                // 忽略获取的header数据
                $ignore = array('host','accept','content-length','content-type');
                $headers = array();
          
                foreach($_SERVER as $key=>$value){
                    if(substr($key, 0, 5)==='HTTP_'){
                        $key = substr($key, 5);
                        $key = str_replace('_', ' ', $key);
                        $key = str_replace(' ', '-', $key);
                        $key = strtolower($key);

                       // if(!in_array($key, $ignore)){
                            $headers[$key] = $value;
                       // }
                    }
                }
                return $headers;
            }
       public function em_getallheaders()
    {
        foreach ($_SERVER as $name => $value)
        {
            if (substr($name, 0, 5) == 'HTTP_')
            {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }

猜你喜欢

转载自blog.csdn.net/wyj1122/article/details/80345588