/** * http 请求 * @param string $url * @param array $params * @param string $data * @return string */ public static function http($url, $params = array(), $data = null) { $curl = curl_init(); if (empty($data)) { $body = ''; if (!empty($params)) { if (is_array($params)) { $body = http_build_query($params); } } } else { $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params); $body = $data; } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_TIMEOUT, self::$_TIME_OUT); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, self::$_TIME_OUT); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $body); curl_setopt($curl, CURLOPT_SSLVERSION, 1); //升级ssl curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //$urlArr = parse_url($url); //$port = empty($urlArr['port']) ? 80 : $urlArr['port']; //curl_setopt($curl, CURLOPT_PORT, $port); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //获取的信息以文件流的形式返回,不直接输出 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); self::$_HTTP_CODE = curl_getinfo($curl, CURLINFO_HTTP_CODE); self::$_HTTP_INFO = curl_getinfo($curl); self::$_HTTP_ERROR_CODE = curl_errno($curl); self::$_HTTP_ERROR = curl_error($curl); curl_close($curl); self::$_HTTP_RESPONSE = $response; return $response; }
/** * 上传图片 */ public function upload() { // printJson('http://pic.baidu.com/upload/web/images/201603/56f9e14d508141496.jpg'); $file = $_FILES['file']; if (empty($file)) { printJson(null, 1, "未上传文件.请重试"); } $suffix = pathinfo($file['name'], PATHINFO_EXTENSION); $suffix = strtolower($suffix); if (!in_array($suffix, array('jpg', 'png', 'bmp', 'gif', 'jpeg'))) { printJson(null, 1, "上传文件格式不被支持"); } $file = file_get_contents($file['tmp_name']); $url = "http://pic.baidu.com/task/task.php?opt=uploadPic"; $res = RequestClient::http($url, null, array('block' => 'actbao', 'pic' => base64_encode($file))); Logger::debug("上传comment结果返回" . $res, RequestClient::getError()); if (empty($res) || $res == '500') { printJson(null, 1, "上传失败,请重试"); } $res = json_decode($res, true); if (!isset($res['url'])) { printJson(null, 1, "上传失败.请重试"); } printJson($res['url']); }
/** * 向第三方push 数据 * @param string $apiKey * @param string $apiSecret * @param string $type ThirdPartyPushType 中定义 * @param string $message 数据采集子系统客户上行原始信息(xml) * @param string $openId * @param unknown_type $oparId */ function pushToThirdParty ($apiKey, $apiSecret, $url, $type, $message, $openId = '', $oparId = 0) { if (!class_exists("RequestClient")) { include_once dirname(__FILE__) . '/../Http/RequestClient.class.php'; } $url = trim($url,'&'); $param = getAuthQueryData($apiKey, $apiSecret); $param['type'] = $type; switch ($type) { case PushToThirdpartyKey::CLOSE_SESSION://关闭会话 $param['openid'] = $openId; $param['operatorid'] = $oparId; $postStr = ''; break; case PushToThirdpartyKey::TEXT: case PushToThirdpartyKey::IMAGE: case PushToThirdpartyKey::LOCATION: case PushToThirdpartyKey::SUBSCRIBE: case PushToThirdpartyKey::UNSUBSCRIBE: $postStr = $message; break; default: return false; } $url = sprintf($url.'%s'.http_build_query($param), ((strrpos($url, '?') === false)?'?':'&')); RequestClient::request(RequestClient::POST, $url, 80, array(), $postStr); if (RequestClient::$httpCode != 200 && class_exists("Logger")) { Logger::error("pushToThirdParty error from base function", RequestClient::$httpInfo); return false; } return true; }
public static function setAccess() { self::$_access = true; }