模拟POST请求
public static post ( string $url, array $fields, string $data_type = 'text' ) : mixed | ||
$url | string | |
$fields | array | |
$data_type | string | |
리턴 | mixed | Examples: ``` HttpCurl::post('http://api.example.com/?a=123', array('abc'=>'123', 'efg'=>'567'), 'json'); HttpCurl::post('http://api.example.com/', '这是post原始内容', 'json'); 文件post上传 HttpCurl::post('http://api.example.com/', array('abc'=>'123', 'file1'=>'@/data/1.jpg'), 'json'); ``` |
/** * 长链接转短链接接口 * * @string $long_url 需要转换的长链接,支持http://、https://、weixin://wxpay 格式的url * * @return array(err, data) * - `err`, 调用失败时得到的异常 * - `res`, 调用正常时得到的对象 * * Examples: * ``` * list($err, $data) = $api->shorturl('http://me.diary8.com/category/web-front-end.html'); * echo $data->short_url; * ``` * Result: * ``` * http://w.url.cn/s/ABJrkxE * ``` */ public function shorturl($long_url) { $url = self::API_DOMAIN . 'cgi-bin/shorturl?access_token=' . $this->get_access_token(); $xml = '{"action":"long2short","long_url":"' . $long_url . '"}'; $res = HttpCurl::post($url, $xml, 'json'); // 异常处理: 获取时网络错误 if ($res === FALSE) { return Error::code('ERR_GET'); } // 判断是否调用成功 if ($res->errcode == 0) { return array(NULL, $res); } else { return array($res, NULL); } }
/** * 设置用户备注名 * * Examples: * ``` * $api->update_user_remark('ocNtAt0YPGDme5tJBXyTphvrQIrc', '用户的备注名'); * ``` * Result: * ``` * [ * null, * { * errcode: 0, * errmsg: "ok" * } * ] * ``` * * @param string $open_id [用户标识] * @param string $remark [新的备注名,长度必须小于30字符] * * @return array(err, data) * - `err`, 调用失败时得到的异常 * - `res`, 调用正常时得到的对象 */ public function update_user_remark($open_id, $remark) { $url = self::API_DOMAIN . 'cgi-bin/user/info/updateremark?access_token=' . $this->get_access_token(); $xml = sprintf('{"openid":"%s", "remark":"%s"}', $open_id, $remark); $res = HttpCurl::post($url, $xml, 'json'); // 异常处理: 获取时网络错误 if ($res === false) { return Error::code('ERR_POST'); } // 判断是否调用成功 if ($res->errcode == 0) { return array(null, $res); } else { return array($res, null); } }
/** * 微信支付 - 生成预订单 * @param string $openid 用户openid * @param array $conf 配置数组 * @return bool|mixed */ public function wxPayUnifiedOrder($openid, $conf = []) { $input = ['out_trade_no' => $conf['out_trade_no'], 'body' => $conf['body'], 'total_fee' => $conf['total_fee'], 'trade_type' => isset($conf['trade_type']) ? $conf['trade_type'] : 'JSAPI', 'notify_url' => isset($conf['notify_url']) ? $conf['notify_url'] : '', 'attach' => isset($conf['attach']) ? $conf['attach'] : '', 'time_start' => isset($conf['time_start']) ? $conf['time_start'] : date('YmdHis'), 'time_expire' => isset($conf['time_expire']) ? $conf['time_expire'] : date('YmdHis', time() + 86400), 'goods_tag' => isset($conf['goods_tag']) ? $conf['goods_tag'] : '', 'openid' => $openid, 'appid' => $this->appId, 'mch_id' => $this->mchId, 'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], 'nonce_str' => SHA1::get_random_str(32)]; // 签名 $input['sign'] = SHA1::getSign2($input, 'key=' . $this->key); // 生成xml $xml = Xml::toXml($input); // 调用接口 $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; try { $res = HttpCurl::post($url, $xml); libxml_disable_entity_loader(true); return json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true); } catch (\Exception $e) { return false; } }