public function mail($subject, $content, $mailto = []) { $url = "http://120.26.210.193:1218/?name=mail&opt=put"; $curl = new Curl(); if ($mailto) { $to = $mailto; } else { $to = $this->mailto; } if (empty($to)) { throw new \Exception('发送对像不允许为空!'); } if (empty($subject)) { throw new \Exception('发送主题不为空'); } if (empty($content)) { throw new \Exception('发送内容不为空'); } $body = sprintf('%s|%s|%s', implode(',', $to), $subject, $content); return $curl->post($url, $body); }
static function syncUser($access_token, $media_id, $url = '', $token = '', $aeskey = '') { $curl = new Curl(); if (!empty($url) && !empty($token) && !empty($aeskey)) { //此分支是按照协议来的,但是目前为止,没有调通。 by fangl $json = json_encode(['media_id' => $media_id, 'callback' => ['url' => $url, 'token' => $token, 'encodingaeskey' => base64_encode($aeskey)]]); } else { $json = json_encode(['media_id' => $media_id]); } \Yii::info($json, __METHOD__); $ret = $curl->post(str_replace('ACCESS_TOKEN', $access_token, self::SYNC_USER), $json); return json_decode($ret, true); }
/** * 请求Api * @param strign $http_method , post or get * @param string $url , '/orgcode/demo/site/index' * @param array $params ,['id'=>1,'type'=>'test' ,...] ,也可以不传,如果参数在url参数上,这里必须传空 * @param array|string $data post提交数据的时候必须传 * @return string * @throws \Exception */ private function _callApi($http_method, $url, $params = [], $data = []) { // $cache_result = $this->_getFromCache($url,$params); // if($cache_result){ // return $cache_result; // } $curl = new Curl(); if ($this->curl_options) { //设置超时请求 foreach ($this->curl_options as $key => $val) { $curl->setOption($key, $val); //设置curl options } } //将签名参数加入到GET请求里,最终生成一个参数值,从而简化了请求 $params[$this->sign_key] = $this->_getSign($this->appid, $this->appsecret, time()); switch (strtoupper($http_method)) { case 'GET': $result = $curl->get($url, $params, $this->debug); break; case 'POST': $result = $curl->post($url, $data, $params, $this->debug); break; default: throw new \Exception('不支持的HTTP请求方法'); } if ($curl->getError()) { return \mysoft\helpers\String::jsonEncode(['success' => 0, 'data' => $curl->getError()]); } return $result; }
/** * 发送post请求 * @param type $apiPath * @param type $data * @param array $params * @param type $context 请求上下文,比如带状态的请求的taskId,格式['taskId'=>'','logId'=>''] * @return type */ public function post($apiPath, $data, $params = array(), $context = []) { $signature = $this->_getSign($this->_appid, $this->_appsecret, time()); $params[$this->sign_key] = $signature; $params['appid'] = $this->_appid; $curl = new Curl(); if (!isset($this->timeout)) { $this->timeout = 120; } $curl->setOption(CURLOPT_TIMEOUT, $this->timeout); $curl->setOption(CURLOPT_SSL_VERIFYPEER, FALSE); $curl->setOption(CURLOPT_SSL_VERIFYHOST, FALSE); $url = $this->_buildUrl($apiPath); $postData = ['url' => $url, 'data' => $data, 'params' => $params]; $startTime = microtime(true); $context['bgnTime'] = date('Y-m-d H:i:s'); $receiveData = $curl->post($url, \mysoft\helpers\String::jsonEncode($data), $params); $context['endTime'] = date('Y-m-d H:i:s'); $context['requestTime'] = (microtime(true) - $startTime) * 1000; return $this->_handleReceiveData($curl, $postData, $receiveData, $context); }