php curl操作API接口类完整示例
本文实例讲述了phpcurl操作API接口类。分享给大家供大家参考,具体如下:
requestType=strtolower($requestType); $paramUrl=''; //PATHINFO模式 if(!empty($data)){ foreach($dataas$key=>$value){ $paramUrl.=$key.'='.$value.'&'; } $url=$url.'?'.$paramUrl; } //初始化类中的数据 $this->url=$url; $this->data=$data; try{ if(!$this->curl=curl_init()){ thrownewException('curl初始化错误:'); }; }catch(Exception$e){ echo''; print_r($e->getMessage()); echo''; } curl_setopt($this->curl,CURLOPT_URL,$this->url); curl_setopt($this->curl,CURLOPT_RETURNTRANSFER,1); //curl_setopt($this->curl,CURLOPT_HEADER,1); } /** *[_post设置get请求的参数] *@return[type][description] */ publicfunction_get(){ } /** *[_post设置post请求的参数] *post新增资源 *@return[type][description] */ publicfunction_post(){ curl_setopt($this->curl,CURLOPT_POST,1); curl_setopt($this->curl,CURLOPT_POSTFIELDS,$this->data); } /** *[_put设置put请求] *put更新资源 *@return[type][description] */ publicfunction_put(){ curl_setopt($this->curl,CURLOPT_CUSTOMREQUEST,'PUT'); } /** *[_delete删除资源] *delete删除资源 *@return[type][description] */ publicfunction_delete(){ curl_setopt($this->curl,CURLOPT_CUSTOMREQUEST,'DELETE'); } /** *[doRequest执行发送请求] *@return[type][description] */ publicfunctiondoRequest(){ //发送给服务端验证信息 if((null!==self::token)&&self::token){ $this->headers=array( 'Client-Token:'.self::token,//此处不能用下划线 'Client-Code:'.$this->setAuthorization() ); } //发送头部信息 $this->setHeader(); //发送请求方式 switch($this->requestType){ case'post': $this->_post(); break; case'put': $this->_put(); break; case'delete': $this->_delete(); break; default: curl_setopt($this->curl,CURLOPT_HTTPGET,TRUE); break; } //执行curl请求 $info=curl_exec($this->curl); //获取curl执行状态信息 $this->status=$this->getInfo(); returnjson_decode($info); } /** *设置发送的头部信息 */ privatefunctionsetHeader(){ curl_setopt($this->curl,CURLOPT_HTTPHEADER,$this->headers); } /** *生成授权码 *@returnstring授权码 */ privatefunctionsetAuthorization(){ $authorization=md5(substr(md5(self::token),8,24).self::token); return$authorization; } /** *获取curl中的状态信息 */ publicfunctiongetInfo(){ returncurl_getinfo($this->curl); } /** *关闭curl连接 */ publicfunction__destruct(){ curl_close($this->curl); } }
更多关于PHP相关内容感兴趣的读者可查看本站专题:《phpcurl用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》及《PHP中json格式数据操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。