function load($url, $data, $post = false, $dataType = 'json', $headers = []) { $length = 0; $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, false); if ($post) { if (isset($data['key'])) { $url .= '?key=' . $data['key']; unset($data['key']); } if ($dataType == 'json') { $data = json_encode($data); } $length = strlen($data); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } else { curl_setopt($ch, CURLOPT_URL, $url . '?' . arrayToQuery($data)); } curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(['User-Agent: APILoader (' . $_SERVER['HTTP_HOST'] . ')', 'Accept-Language: ru-RU,ru;q=0.9', 'Accept-Charset: utf-8', 'Accept-Encoding: deflate', 'Content-Type: application/json', 'Accept: application/json', 'Content-length: ' . $length, 'Cache-Control: no-cache', 'Connection: Keep-Alive'], $headers)); $result = curl_exec($ch); curl_close($ch); return $result; }
function prepareSearch($text) { $text = addslashes(preg_replace("/[^A-Za-z0-9 \\']/", "", $text)); $new = explode(" ", $text); $final = arrayToQuery($new, "title") . ' OR ' . arrayToQuery($new, "article"); return $final; }
/** * Create parameter strng from array * @param array ar parameter array * @param string prefix strings for the prefix for key * @return string parameter string */ function arrayToQuery($ar, $prefix) { if (is_array($ar)) { $items = array(); foreach ($ar as $key => $value) { $items[] = arrayToQuery($value, "{$prefix}_{$key}"); } $returnStr = implode('', $items); } else { $returnStr = "&{$prefix}=" . urlencode($ar); } return $returnStr; }
/** * 获取个应用系统登录接口地址 * * @param bool $is_register true 表示注册 * @return array|null * @throws \yii\base\Exception */ public function getCookieUrl($is_register = false) { $cookieUrl = null; $appId = null; $info = null; $userModel = new UserBaseInfo(); $userInfo = $userModel->getUserInfoCache(); $sso = SsoSettingInfo::findAll(); if ($sso) { foreach ($sso as $k => $val) { if (isset($val['log_out']['sign_in']) && $val['log_out']['sign_in']) { $cookieUrl[] = $val['log_out']['sign_in']; $appId[] = $val['app_id']; } } } if (isset($userInfo['id']) && $userInfo['id'] && $cookieUrl) { $user = $userModel->getUserById($userInfo['id']); $info['openid'] = $user['open_id']; $info['mobile'] = $user['mobile']; $info['email'] = $user['email']; if ($is_register) { $info['is_register'] = $is_register; $info['salt'] = $user['password_salt']; $info['password'] = $user['password']; } $info = json_encode($info); foreach ($cookieUrl as $key => &$url) { $appDir = $appId[$key]; $encrypt = AsymmetryCrypt::opensslPublicEncrypt($info, dirname(Yii::$app->getBasePath()) . '/common/rsa_key/' . $appDir . '/rsa_public_key.pem'); // TODO:: 为解决 +/ 丢失 / bug,临时解决方案 $encrypt = str_replace('+/', '+_', $encrypt); $encrypt = rawurlencode($encrypt); $bool = filter_var($url, FILTER_VALIDATE_URL); if ($bool === false) { unset($cookieUrl[$key]); continue; } $path = parse_url($url); if (!isset($path['query'])) { $url .= '?c=' . $encrypt; continue; } $query = queryToArray($path['query']); $query['c'] = $encrypt; $query_params = arrayToQuery($query); $url = $path['scheme'] . '://' . $path['host'] . '?' . $query_params; } shuffle($cookieUrl); return $cookieUrl; } else { return null; } }
/** * 二维码支付 */ public function actionNative() { $request = Yii::$app->getRequest(); $params = $request->get(); if (!isset($params['signature']) || !$params['signature']) { // TODO:: 必须传递签名参数 return; } $signature = rawurldecode($params['signature']); unset($params['signature']); $data = arrayToQuery($params); $file = dirname(Yii::$app->getBasePath()) . '/common/rsa_key/signature/public_key.pem'; $result = AsymmetryCrypt::verifySign($data, $signature, $file); if (!$result) { return; } $notifyUrl = Url::toRoute(['wxpay/notify'], true); $notify = new NativePay(); $url = ''; //模式一 /** * 流程: * 1、组装包含支付信息的url,生成二维码 * 2、用户扫描二维码,进行支付 * 3、确定支付之后,微信服务器会回调预先配置的回调地址,在【微信开放平台-微信支付-支付配置】中进行配置 * 4、在接到回调通知之后,用户进行统一下单支付,并返回支付信息以完成支付(见:native_notify.php) * 5、支付完成之后,微信服务器会通知支付成功 * 6、在支付成功通知中需要查单确认是否真正支付成功(见:notify.php) */ //$url = $notify->GetPrePayUrl("123456789"); //模式二 /** * 流程: * 1、调用统一下单,取得code_url,生成二维码 * 2、用户扫描二维码,进行支付 * 3、支付完成之后,微信服务器会通知支付成功 * 4、在支付成功通知中需要查单确认是否真正支付成功(见:notify.php) */ $input = new WxPayUnifiedOrder(); $input->SetBody($params['product_name']); if (isset($params['attach'])) { $input->SetAttach($params['attach']); } $input->SetOut_trade_no($params['order_sn']); $input->SetTotal_fee($params['turnover'] * 100); $input->SetTime_start(date('YmdHis', $params['create_time'])); $input->SetTime_expire(date('YmdHis', $params['expires_time'])); if (isset($params['product_desc'])) { $input->SetGoods_tag($params['product_desc']); } $input->SetNotify_url($notifyUrl); $input->SetTrade_type("NATIVE"); $input->SetProduct_id($params['product_id']); $result = $notify->GetPayUrl($input); $url = $result["code_url"]; $qrCode = new QrCode(); $qrCode->setText($url)->setSize(100)->setPadding(10)->setErrorCorrection('high')->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))->setLabel('')->setLabelFontSize(16)->render(); }
function insertUpdate($table, $cols, $vals, $compField, $compCond) { if (singlefield($compField, $table, $compCond)) { // echo $compField.",".$compCond; $col_val = arrayToQuery($cols, $vals); if (updaterecs($table, $col_val, $compCond)) { $arr = array('status' => 1, 'message' => 'Sync Success', 'sync_time' => date("d/m/Y h:i:sa")); } else { $arr = array('status' => 0, 'message' => 'Sync Failure. Please try again'); } } else { $fields = rtrim(implode(',', $cols), ','); $vals = rtrim(implode(',', $vals), ','); if (insertrec($table, $fields, $vals)) { $arr = array('status' => 1, 'message' => 'Sync Success', 'sync_time' => date("d/m/Y h:i:sa")); } else { $arr = array('status' => 0, 'message' => 'Sync Failure. Please try again'); } } return $arr; }