ci = &get_instance(); if (!is_product()) { $this->api_url = 'https://api.ss.haodian.cn/hd/app/'; //测试地址 } $this->ci->load->helper('string'); $this->log_path = 'ss_api.log'; } /** * @param $id * @param $name * @param $mobile * @param $en_time * @param $remark * @param $to_customers 是否添加到客户池 * @param $biz_id * @return array */ public function postClues($id, $name, $mobile, $en_time, $remark, $to_customers = 0, $biz_id = 0) { $data = [ 'name' => $name, 'mobile' => $mobile, 'en_time' => $en_time, 'out_id' => $id, 'cf_platform' => self::CF_PLATFORM, 'biz_id' => $biz_id, 'to_customers' => $to_customers, 'remark' => $remark, 'nonce_str' => random_string('alpha'), 'app_id' => $this->app_id, ]; $data['sign'] = $this->sign($data); $client = new GuzzleHttp\Client(); $options = [ \GuzzleHttp\RequestOptions::HEADERS => ['Content-Type' => 'application/json'], \GuzzleHttp\RequestOptions::JSON => $data, ]; $url = $this->api_url . self::CLUES_METHOD; try { debug_log("[info]#请求地址:" . $url, $this->log_path); debug_log("[info]#请求参数:" . json_encode($data, JSON_UNESCAPED_UNICODE), $this->log_path); $response = $client->post($url, $options); debug_log("[info]#返回信息:" . $response->getBody(), $this->log_path); if ($response->getStatusCode() != 200) { debug_log("[error]#" . $response->getStatusCode(), $this->log_path); return ['code' => 0, 'msg' => '网络错误:' . $response->getStatusCode()]; } $reqInfo = json_decode($response->getBody()); if (!$reqInfo || $reqInfo->code != 200) { $msg = $reqInfo ? $reqInfo->msg : ''; return ['code' => 0, 'msg' => '保存失败:' . $msg]; } return ['code' => 1, 'msg' => '保存成功']; } catch (Exception $e) { debug_log("[error]#" . $e->getMessage(), $this->log_path); return ['code' => 0, 'msg' => $e->getMessage()]; } } public function getStatus($out_ids) { if (is_array($out_ids)) { $out_ids = implode(',', $out_ids); } $data = [ 'out_ids' => $out_ids, 'cf_platform' => self::CF_PLATFORM, 'nonce_str' => random_string('alpha'), 'app_id' => $this->app_id, ]; $data['sign'] = $this->sign($data); $client = new GuzzleHttp\Client(); $options = [ \GuzzleHttp\RequestOptions::HEADERS => ['Content-Type' => 'application/json'], \GuzzleHttp\RequestOptions::JSON => $data, ]; $url = $this->api_url . self::STATUS_METHOD; try { debug_log("[info]#请求地址:" . $url, $this->log_path); debug_log("[info]#请求参数:" . json_encode($data, JSON_UNESCAPED_UNICODE), $this->log_path); $response = $client->post($url, $options); debug_log("[info]#返回信息:" . $response->getBody(), $this->log_path); if ($response->getStatusCode() != 200) { debug_log("[error]#" . $response->getStatusCode(), $this->log_path); return ['code' => 0, 'msg' => '网络错误:' . $response->getStatusCode()]; } $reqInfo = json_decode($response->getBody(), true); if (!$reqInfo || $reqInfo['code'] != 200) { $msg = $reqInfo ? $reqInfo['msg'] : ''; return ['code' => 0, 'msg' => '保存失败:' . $msg]; } return ['code' => 1, 'msg' => '保存成功', 'data' => $reqInfo['data']]; } catch (Exception $e) { debug_log("[error]#" . $e->getMessage(), $this->log_path); return ['code' => 0, 'msg' => $e->getMessage()]; } } public function getBiz($biz_id, $cache = true) { $cacheKey = "SYSTOPIC_SSAPI_BIZ_{$biz_id}"; $redis = &load_cache(); if ($cache) { $bizInfo = $redis->get($cacheKey); if ($bizInfo) { return ['code' => 1, 'msg' => 'success', 'data' => $bizInfo]; } } //数据缓存 $data = [ 'biz_id' => $biz_id, 'nonce_str' => random_string('alpha'), 'app_id' => $this->app_id, ]; $data['sign'] = $this->sign($data); $client = new GuzzleHttp\Client(); $options = [ \GuzzleHttp\RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]; $url = $this->api_url . self::BIZ_METHOD . '?' . http_build_query($data); try { debug_log("[info]#请求地址:" . $url, $this->log_path); $response = $client->get($url, $options); debug_log("[info]#返回信息:" . $response->getBody(), $this->log_path); if ($response->getStatusCode() != 200) { debug_log("[error]#" . $response->getStatusCode(), $this->log_path); return ['code' => 0, 'msg' => '网络错误:' . $response->getStatusCode()]; } $reqInfo = json_decode($response->getBody(), true); if (!$reqInfo || $reqInfo['code'] != 200) { $msg = $reqInfo ? $reqInfo['msg'] : ''; return ['code' => 0, 'msg' => '请求失败:' . $msg]; } $redis->save($cacheKey, $reqInfo['data'], 60 * 60); return ['code' => 1, 'msg' => 'success', 'data' => $reqInfo['data']]; } catch (Exception $e) { debug_log("[error]#" . $e->getMessage(), $this->log_path); return ['code' => 0, 'msg' => $e->getMessage()]; } } private function sign($param) { unset($param['sign']); //按字典序排序参数 ksort($param); $buff = ""; foreach ($param as $k => $v) { if (!is_array($v) && strlen($v) > 0) { $buff .= $k . "=" . ($v) . "&"; } } $buff = trim($buff, "&"); $buff = $buff . "&key=" . $this->sign_key; return strtoupper(md5($buff)); } }