




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、urlencode($key) ,num=urlencode($num) ,show=urlencode($show) ,);foreach($fields as $key=$value)$fields_string .= $key.=.$value.& ;/把变量组装成username=mayuchao&password=123456&成功了mayuchao密码laoma 这个格式rtrim($fields_string ,&) ; /从末端开始去掉删除多余&符号$ch = curl_init() ; / 开启一个curl对话curl_setopt($ch, curlopt_url,$url
2、) ; /把要对话的url地址设置好curl_setopt($ch, curlopt_post,count($fields) ; /把要发送的变量数量设置好curl_setopt($ch, curlopt_postfields,$fields_string) ; /把要post的变量url组装好化后设置好curl_exec($ch) ; /执行上面设置好的一个curl操作,把变量数据发送完后,返回的是ok.php页面获得post变量执行后的结果curl_close($ch) ; /关闭curl对话?secretkey;foreach ($params as $k = $v)if( != sub
3、str($v, 0, 1)$stringtobesigned .= $k$v;unset($k, $v);$stringtobesigned .= $this-secretkey;return strtoupper(md5($stringtobesigned);protected function curl($url, $postfields = null)$ch = curl_init();curl_setopt($ch, curlopt_url, $url);curl_setopt($ch, curlopt_failonerror, false);curl_setopt($ch, curl
4、opt_returntransfer, true);if (is_array($postfields) & 0 $v)if( != substr($v, 0, 1)/判断是不是文件上传$postbodystring .= $k= . urlencode($v) . &; else/文件上传用multipart/form-data,否则用www-form-urlencoded$postmultipart = true;unset($k, $v);curl_setopt($ch, curlopt_post, true);if ($postmultipart)curl_setopt($ch, cur
5、lopt_postfields, $postfields);elsecurl_setopt($ch, curlopt_postfields, substr($postbodystring,0,-1);$reponse = curl_exec($ch);if (curl_errno($ch)throw new exception(curl_error($ch),0);else$httpstatuscode = curl_getinfo($ch, curlinfo_http_code);if (200 != $httpstatuscode)throw new exception($reponse,
6、$httpstatuscode);curl_close($ch);return $reponse;protected function logcommunicationerror($apiname, $requesturl, $errorcode, $responsetxt)$localip = isset($_serverserver_addr) ? $_serverserver_addr : cli;$logger = new ltlogger;$logger-conflog_file = rtrim(top_sdk_work_dir, /) . / . logs/top_comm_err
7、_ . $this-appkey . _ . date(y-m-d) . .log;$logger-confseparator = _;$logdata = array(date(y-m-d h:i:s),$apiname,$this-appkey,$localip,php_os,$this-sdkversion,$requesturl,$errorcode,str_replace(n,$responsetxt);$logger-log($logdata);public function execute($request, $session = null)if($this-checkreque
8、st) try $request-check(); catch (exception $e) $result-code = $e-getcode();$result-msg = $e-getmessage();return $result;/组装系统参数$sysparamsapp_key = $this-appkey;$sysparamsv = $this-apiversion;$sysparamsformat = $this-format;$sysparamssign_method = $this-signmethod;$sysparamsmethod = $request-getapime
9、thodname();$sysparamstimestamp = date(y-m-d h:i:s);$sysparamspartner_id = $this-sdkversion;if (null != $session)$sysparamssession = $session;/获取业务参数$apiparams = $request-getapiparas();/签名$sysparamssign = $this-generatesign(array_merge($apiparams, $sysparams);/系统参数放入get请求串$requesturl = $this-gatewayu
10、rl . ?;foreach ($sysparams as $sysparamkey = $sysparamvalue)$requesturl .= $sysparamkey= . urlencode($sysparamvalue) . &;$requesturl = substr($requesturl, 0, -1);/发起http请求try$resp = $this-curl($requesturl, $apiparams);catch (exception $e)$this-logcommunicationerror($sysparamsmethod,$requesturl,http_
11、error_ . $e-getcode(),$e-getmessage();$result-code = $e-getcode();$result-msg = $e-getmessage();return $result;/解析top返回结果$respwellformed = false;if (json = $this-format)$respobject = json_decode($resp);if (null != $respobject)$respwellformed = true;foreach ($respobject as $propkey = $propvalue)$resp
12、object = $propvalue;else if(xml = $this-format)$respobject = simplexml_load_string($resp);if (false != $respobject)$respwellformed = true;/返回的http文本不是标准json或者xml,记下错误日志if (false = $respwellformed)$this-logcommunicationerror($sysparamsmethod,$requesturl,http_response_not_well_formed,$resp);$result-co
13、de = 0;$result-msg = http_response_not_well_formed;return $result;/如果top返回了错误码,记录到业务错误日志中if (isset($respobject-code)$logger = new ltlogger;$logger-conflog_file = rtrim(top_sdk_work_dir, /) . / . logs/top_biz_err_ . $this-appkey . _ . date(y-m-d) . .log;$logger-log(array(date(y-m-d h:i:s),$resp);retu
14、rn $respobject;public function exec($paramsarray)if (!isset($paramsarraymethod)trigger_error(no api name passed);$inflector = new ltinflector;$inflector-confseparator = .;$requestclassname = ucfirst($inflector-camelize(substr($paramsarraymethod, 7) . request;if (!class_exists($requestclassname)trigg
15、er_error(no such api: . $paramsarraymethod);$session = isset($paramsarraysession) ? $paramsarraysession : null;$req = new $requestclassname;foreach($paramsarray as $parakey = $paravalue)$inflector-confseparator = _;$settermethodname = $inflector-camelize($parakey);$inflector-confseparator = .;$sette
16、rmethodname = set . $inflector-camelize($settermethodname);if (method_exists($req, $settermethodname)$req-$settermethodname($paravalue);return $this-execute($req, $session);/http 认证:用户名和密码认证以下为引用的内容:/ 创建两个curl资源$ch1 = curl_init();$ch2 = curl_init();/ 指定url和适当的参数curl_setopt($ch1, curlopt_url, http:/l
17、/);curl_setopt($ch1, curlopt_header, 0);curl_setopt($ch2, curlopt_url, /);curl_setopt($ch2, curlopt_header, 0);/ 创建curl批处理句柄$mh = curl_multi_init();/ 加上前面两个资源句柄curl_multi_add_handle($mh,$ch1);curl_multi_add_handle($mh,$ch2);/ 预定义一个状态变量$active = null;/ 执行批处理do $mrc = curl_m
18、ulti_exec($mh, $active); while ($mrc = curlm_call_multi_perform);while ($active & $mrc = curlm_ok) if (curl_multi_select($mh) != -1) do $mrc = curl_multi_exec($mh, $active); while ($mrc = curlm_call_multi_perform); / 关闭各个句柄curl_multi_remove_handle($mh, $ch1);curl_multi_remove_handle($mh, $ch2);curl_
19、multi_close($mh); 这里要做的就是打开多个curl句柄并指派给一个批处理句柄。然后你就只需在一个while循环里等它执行完毕。这个示例中有两个主要循环。第一个 do-while 循环重复调用 curl_multi_exec() 。这个函数是无隔断(non-blocking)的,但会尽可能少地执行。它返回一个状态值,只要这个值等于常量 curlm_call_multi_perform ,就代表还有一些刻不容缓的工作要做(例如,把对应url的http头信息发送出去)。也就是说,我们需要不断调用该函数,直到返回值发生改变。而接下来的 while 循环,只在 $active 变量为 t
20、rue 时继续。这一变量之前作为第二个参数传给了 curl_multi_exec() ,代表只要批处理句柄中是否还有活动连接。接着,我们调用 curl_multi_select() ,在活动连接(例如接受服务器响应)出现之前,它都是被“屏蔽”的。这个函数成功执行后,我们又会进入另一个 do-while 循环,继续下一条url?php/*snoopy - the php net clientauthor: monte ohrt copyright (c): 1999-2000 ispi, all rights reservedversion: 1.01 * this library is fre
21、e software; you can redistribute it and/or * modify it under the terms of the gnu lesser general public * license as published by the free software foundation; either * version 2.1 of the license, or (at your option) any later version. * * this library is distributed in the hope that it will be usef
22、ul, * but without any warranty; without even the implied warranty of * merchantability or fitness for a particular purpose. see the gnu * lesser general public license for more details. * * you should have received a copy of the gnu lesser general public * license along with this library; if not, wr
23、ite to the free software * foundation, inc., 59 temple place, suite 330, boston, ma 02111-1307 usayou may contact the author of snoopy by e-mail at:or, write to:monte ohrtcto, ispi237 s. 70th suite 220lincoln, ne 68510the latest version of snoopy can be obtained from:http:/snoopy.source
24、/*/class snoopy/* public variables */* user definable vars */var $host=;/ host name we are connecting tovar $port=80;/ port we are connecting tovar $proxy_host=;/ proxy host to usevar $proxy_port=;/ proxy port to usevar $proxy_user=;/ proxy user to usevar $proxy_pass=;/ proxy pas
25、sword to usevar $agent=snoopy v1.2.3;/ agent we masquerade asvar$referer=;/ referer info to passvar $cookies=array();/ array of cookies to pass/ $cookiesusername=joe;var$rawheaders=array();/ array of raw headers to send/ $rawheaderscontent-type=text/html;var $maxredirs=5;/ http redirection depth max
26、imum. 0 = disallowvar $lastredirectaddr=;/ contains address of last redirected addressvar$offsiteok=true;/ allows redirection off-sitevar $maxframes=0;/ frame content depth maximum. 0 = disallowvar $expandlinks=true;/ expand links to fully qualified urls./ this only applies to fetchlinks()/ submitli
27、nks(), and submittext()var $passcookies=true;/ pass set cookies back through redirects/ note: this currently does not respect/ dates, domains or paths.var$user=;/ user for http authenticationvar$pass=;/ password for http authentication/ http accept typesvar $accept=image/gif, image/x-xbitmap, image/
28、jpeg, image/pjpeg, */*;var $results=;/ where the content is putvar $error=;/ error messages sent herevar$response_code=;/ response code returned from servervar$headers=array();/ headers returned from server sent herevar$maxlength=500000;/ max return data length (body)var $read_timeout=0;/ timeout on
29、 read operations, in seconds/ supported only since php 4 beta 4/ set to 0 to disallow timeoutsvar $timed_out=false;/ if a read operation timed outvar$status=0;/ http request statusvar $temp_dir=/tmp;/ temporary directory that the webserver/ has permission to write to./ under windows, this should be
30、c:tempvar$curl_path=/usr/local/bin/curl;/ snoopy will use curl for fetching/ ssl content if a full system path to/ the curl binary is supplied here./ set to false if you do not have/ curl installed. see http:/curl.haxx.se/ for details on installing curl./ snoopy does *not* use the curl/ library func
31、tions built into php,/ as these functions are not stable/ as of this snoopy release./* private variables */var$_maxlinelen=4096;/ max line length (headers)var $_httpmethod=get;/ default http request methodvar $_httpversion=http/1.0;/ default http request versionvar $_submit_method=post;/ default sub
32、mit methodvar $_submit_type=application/x-www-form-urlencoded;/ default submit typevar $_mime_boundary= ;/ mime boundary for multipart/form-data submit typevar $_redirectaddr=false;/ will be set if page fetched is a redirectvar $_redirectdepth=0;/ increments on an http redirectvar $_frameurls= array
33、();/ frame src urlsvar $_framedepth=0;/ increments on frame depthvar $_isproxy=false;/ set if using a proxy servervar $_fp_timeout=30;/ timeout for socket connection/*=*function:fetchpurpose:fetch the contents of a web page(and possibly other protocols in thefuture like ftp, nntp, gopher, etc.)input
34、:$urithe location of the page to fetchoutput:$this-resultsthe output text from the fetch*=*/function fetch($uri)/preg_match(|(:+):/(:/+)(:d+)*(.*)|,$uri,$uri_parts);$uri_parts = parse_url($uri);if (!empty($uri_partsuser)$this-user = $uri_partsuser;if (!empty($uri_partspass)$this-pass = $uri_partspas
35、s;if (empty($uri_partsquery)$uri_partsquery = ;if (empty($uri_partspath)$uri_partspath = ;switch(strtolower($uri_partsscheme)case http:$this-host = $uri_partshost;if(!empty($uri_partsport)$this-port = $uri_partsport;if($this-_connect($fp)if($this-_isproxy)/ using proxy, send entire uri$this-_httpreq
36、uest($uri,$fp,$uri,$this-_httpmethod);else$path = $uri_partspath.($uri_partsquery ? ?.$uri_partsquery : );/ no proxy, send only the path$this-_httprequest($path, $fp, $uri, $this-_httpmethod);$this-_disconnect($fp);if($this-_redirectaddr)/* url was redirected, check if weve hit the max depth */if($t
37、his-maxredirs $this-_redirectdepth)/ only follow redirect if its on this site, or offsiteok is trueif(preg_match(|http:/.preg_quote($this-host).|i,$this-_redirectaddr) | $this-offsiteok)/* follow the redirect */$this-_redirectdepth+;$this-lastredirectaddr=$this-_redirectaddr;$this-fetch($this-_redir
38、ectaddr);if($this-_framedepth maxframes & count($this-_frameurls) 0)$frameurls = $this-_frameurls;$this-_frameurls = array();while(list(,$frameurl) = each($frameurls)if($this-_framedepth maxframes)$this-fetch($frameurl);$this-_framedepth+;elsebreak;elsereturn false;return true;break;case https:if(!$
39、this-curl_path)return false;if(function_exists(is_executable) if (!is_executable($this-curl_path) return false;$this-host = $uri_partshost;if(!empty($uri_partsport)$this-port = $uri_partsport;if($this-_isproxy)/ using proxy, send entire uri$this-_httpsrequest($uri,$uri,$this-_httpmethod);else$path = $uri_partspath.($uri_partsquery ? ?.$uri_partsquery : );/ no proxy, send only the path$this-_httpsrequest($path, $uri, $this-_httpmethod);if($this-_redirectaddr)/* url was redirected, check if weve hit the max depth */if($this-maxred
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高效能源供应链采购油品合同模板
- 浙江省绍兴市嵊州市2025年八年级下学期期末数学试题及参考答案
- 离婚起诉书范文孩子抚养权(15篇)
- 医院餐厅刷卡管理制度
- 劳动合同日常管理制度
- 行政组织的战略管理与组织创新分析试题及答案
- 软件测试工程师技能提升建议试题及答案
- 计算机二级MySQL GROUP BY 使用方法试题及答案
- 医学影像学实践技能考试题集及答案解析
- 汽车维修与保养行业试题
- DB15T 374-2023主要造林树种苗木质量分级
- 子宫颈炎-急性子宫颈炎的诊疗护理(妇产科学课件)
- 医院电子病历系统应用水平分级评价 4级实证材料基础项
- 小学英语-PEP五年级下册 Unit 1My day Read and write教学设计学情分析教材分析课后反思
- 个人课题结题总结报告PPT模板下载
- 高等数学(下)知到章节答案智慧树2023年山东科技大学
- 第一季度胸痛中心典型病例分析会
- 初一语文培优辅差计划和措施15篇
- 自考06393土木工程概论历年(15-19)真题试卷
- The Last of Us《最后生还者(2023)》第一季第八集完整中英文对照剧本
- 高校学生聘用证明模板
评论
0/150
提交评论