PHP笔记:后台get和post带参请求的封装

本文记录了在PHP中如何进行后台GET和POST请求的参数封装,通过示例代码展示了如何使用封装后的httpGet方法。参考链接提供了一个详细的教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

        /*
         * get method
         */
        private function httpGet($url) {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_TIMEOUT, 500);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_URL, $url);
            
            $res = curl_exec($curl);
            curl_close($curl);
            
            return $res;
        }
         
        private function get($url, $param=array()){
            if(!is_array($param)){
                throw new Exception("参数必须为array");
            }
            $p='';
            foreach($param as $key => $value){
                $p=$p.$key.'='.$value.'&';
            }
            if(preg_match('/\?[\d\D]+/',$url)){//matched ?c
                $p='&'.$p;
            }else if(preg_match('/\?$/',$url)){//matched ?$
                $p=$p;
            }else{
                $p='?'.$p;
            }
            $p=preg_replace('/&$/','',$p);
            $url=$url.$p;
            //echo $url;
            $httph =curl_init($url);
            curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1);
            curl_setopt($httph,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
            
            curl_setopt($httph, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($httph, CURLOPT_HEADER,1);
            $rst=curl_exec($httph);
            curl_close($httph);
            return $rst;
        }
        
        //#########################
        /*
         * post method
         */
        private function post($url, $param=array()){
            if(!is_array($param)){
                throw new Exception("参数必须为array");
            }
            $httph =curl_init($url);
            curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1);
            curl_setopt($httph,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
            curl_setopt($httph, CURLOPT_POST, 1);//设置为POST方式 
            curl_setopt($httph, CURLOPT_POSTFIELDS, $param);
            curl_setopt($httph, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($httph, CURLOPT_HEADER,1);
            $rst=curl_exec($httph);
            curl_close($httph);
            return $rst;
        }

 

例:

$res = $this->httpGet($url);

参考地址:http://www.thinkphp.cn/code/357.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值