1、api接口教程phpurlencode($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, CURLO
2、PT_URL,$url) ; /把要对话的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 = $
3、v) if( != substr($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);
4、 curl_setopt($ch, CURLOPT_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 ($post
5、Multipart) curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); else curl_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 != $httpS
6、tatusCode) throw new Exception($reponse,$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 = rtri
7、m(TOP_SDK_WORK_DIR, /) . / . logs/top_comm_err_ . $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 f
8、unction execute($request, $session = null) if($this-checkRequest) 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; $sysPara
9、mssign_method = $this-signMethod; $sysParamsmethod = $request-getApiMethodName(); $sysParamstimestamp = date(Y-m-d H:i:s); $sysParamspartner_id = $this-sdkVersion; if (null != $session) $sysParamssession = $session; /获取业务参数 $apiParams = $request-getApiParas(); /签名 $sysParamssign = $this-generateSign
10、(array_merge($apiParams, $sysParams); /系统参数放入GET请求串 $requestUrl = $this-gatewayUrl . ?; foreach ($sysParams as $sysParamKey = $sysParamValue) $requestUrl .= $sysParamKey= . urlencode($sysParamValue) . &; $requestUrl = substr($requestUrl, 0, -1); /发起HTTP请求 try $resp = $this-curl($requestUrl, $apiPara
11、ms); catch (Exception $e) $this-logCommunicationError($sysParamsmethod,$requestUrl,HTTP_ERROR_ . $e-getCode(),$e-getMessage(); $result-code = $e-getCode(); $result-msg = $e-getMessage(); return $result; /解析TOP返回结果 $respWellFormed = false; if (json = $this-format) $respObject = $resp); if (null != $r
12、espObject) $respWellFormed = true; foreach ($respObject as $propKey = $propValue) $respObject = $propValue; else if(xml = $this-format) $respObject = simplexml_load_string($resp); if (false != $respObject) $respWellFormed = true; /返回的HTTP文本不是标准JSON或者XML,记下错误日志 if (false = $respWellFormed) $this-logC
13、ommunicationError($sysParamsmethod,$requestUrl,HTTP_RESPONSE_NOT_WELL_FORMED,$resp); $result-code = 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
14、/top_biz_err_ . $this-appkey . _ . date(Y-m-d) . .log; $logger-log(array( date(Y-m-d H:i:s), $resp ); return $respObject; public function exec($paramsArray) if (!isset($paramsArraymethod) trigger_error(No api name passed); $inflector = new LtInflector; $inflector-confseparator = .; $requestClassName
15、 = ucfirst($inflector-camelize(substr($paramsArraymethod, 7) . Request; if (!class_exists($requestClassName) trigger_error(No such api: . $paramsArraymethod); $session = isset($paramsArraysession) ? $paramsArraysession : null; $req = new $requestClassName; foreach($paramsArray as $paraKey = $paraVal
16、ue) $inflector-confseparator = _; $setterMethodName = $inflector-camelize($paraKey); $inflector-confseparator = .; $setterMethodName = set . $inflector-camelize($setterMethodName); if (method_exists($req, $setterMethodName) $req-$setterMethodName($paraValue); return $this-execute($req, $session); ?以
17、下为引用的内容:/ 创建两个cURL资源$ch1 = curl_init();$ch2 = curl_init();/ 指定URL和适当的参数curl_setopt($ch1, CURLOPT_URL, 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_a
18、dd_handle($mh,$ch2);/ 预定义一个状态变量$active = null;/ 执行批处理do $mrc = curl_multi_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_mul
19、ti_remove_handle($mh, $ch1);curl_multi_remove_handle($mh, $ch2);curl_multi_close($mh); 这里要做的就是打开多个cURL句柄并指派给一个批处理句柄。然后你就只需在一个while循环里等它执行完毕。这个示例中有两个主要循环。第一个 do-while 循环重复调用 curl_multi_exec() 。这个函数是无隔断(non-blocking)的,但会尽可能少地执行。它返回一个状态值,只要这个值等于常量 CURLM_CALL_MULTI_PERFORM ,就代表还有一些刻不容缓的工作要做(例如,把对应URL的ht
20、tp头信息发送出去)。也就是说,我们需要不断调用该函数,直到返回值发生改变。而接下来的 while 循环,只在 $active 变量为 true 时继续。这一变量之前作为第二个参数传给了 curl_multi_exec() ,代表只要批处理句柄中是否还有活动连接。接着,我们调用 curl_multi_select() ,在活动连接(例如接受服务器响应)出现之前,它都是被“屏蔽”的。这个函数成功执行后,我们又会进入另一个 do-while 循环,继续下一条URL?php/*Snoopy - the PHP net clientAuthor: Monte Ohrt Copyright (c): 1
21、999-2000 ispi, all rights reservedVersion: 1.01 * This library is free 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
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1