注意,操作HTTPS接口时,需要加上,否则会引起内存泄漏
curl_setopt($_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
class CurlExec {
private static $singleinstance = false;
public static function getIntance() {
if (self::$singleinstance == false) {
self::$singleinstance = new self;
}
return self::$singleinstance;
}
function __construct() {}
public function customKSort(&$input) {
ksort($input, SORT_NATURAL);
foreach ($input as &$v) {
if (is_array($v)) {
$this->customKSort($v);
}
}
}
public function buildSign($signData, $appkey) {
try {
if (!is_array($signData)) {
return '';
}
if (isset($signData['sign'])) {
unset($signData['sign']);
}
$this->customKSort($signData);
$sign_str = urldecode(http_build_query($signData));
$sign_str .= '&app_key=' . $appkey;
return strtoupper(md5($sign_str));
} catch (Exception $e) {
Log::info("buildSign Exception: " . $e->getMessage(), Log::EXCEPT);
return false;
}
}
public function get($url, $get_param, $appkey, $sec = Config::s_cur_timeout) {
try {
$get_param['sign'] = $this->buildSign($get_param, $appkey);
$url .= urldecode(http_build_query($get_param));
Log::info("CurlExec::get url = " . $url, Log::INFO);
$_ch = curl_init();
curl_setopt($_ch, CURLOPT_URL, $url);
curl_setopt($_ch, CURLOPT_HEADER, false);
curl_setopt($_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($_ch, CURLOPT_TIMEOUT, $sec);
curl_setopt($_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($_ch);
Log::info("CurlExec::get curl_exec1 = " . $result, Log::INFO);
Log::info("CurlExec::get curl_exec2 = " . json_encode(json_decode($result, true), JSON_UNESCAPED_UNICODE), Log::INFO);
curl_close($_ch);
return $result;
} catch (Exception $e) {
Log::info("get Exception: " . $e->getMessage(), Log::EXCEPT);
return false;
}
}
public function post($url, $get_param, $post_param, $appkey) {
try {
$get_param['sign'] = self::buildSign(array_merge($get_param, $post_param), $appkey);
$url .= urldecode(http_build_query($get_param));
Log::info("CurlExec::post url = " . $url, Log::INFO);
Log::info("CurlExec::post data = " . json_encode($post_param), Log::INFO);
$_ch = curl_init();
curl_setopt($_ch, CURLOPT_URL, $url);
curl_setopt($_ch, CURLOPT_HEADER, false);
curl_setopt($_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($_ch, CURLOPT_TIMEOUT, 3);
curl_setopt($_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($_ch, CURLOPT_POST, true);
curl_setopt($_ch, CURLOPT_POSTFIELDS, json_encode($post_param));
$result = curl_exec($_ch);
Log::info("CurlExec::post curl_exec1 = " . $result, Log::INFO);
Log::info("CurlExec::post curl_exec = " . json_encode(json_decode($result, true), JSON_UNESCAPED_UNICODE), Log::INFO);
curl_close($_ch);
return $result;
} catch (Exception $e) {
Log::info("get Exception: " . $e->getMessage(), Log::EXCEPT);
return false;
}
}
}