Guzzle是一个PHP的HTTP客户端库,用于发送HTTP请求。以下是在PHP代码中使用Guzzle库的基本步骤:
安装Guzzle库:可以使用Composer在命令行中安装Guzzle库。
composer require guzzlehttp/guzzle
导入Guzzle库的命名空间:在PHP代码中导入Guzzle库的命名空间。
use GuzzleHttp\Client;
创建Guzzle客户端:使用Client类创建一个Guzzle客户端对象。
$client = new GuzzleHttp\Client();
public function create_order($data)
{
try {
$client = new Client();
$response = $client->post(
$this->api_url,
[
//post请求
'form_params' => [
'api_key' => $this->api_key,
'pm_id' => $data['pm_id'], //支付方式ID alipay_cn:支付宝 unionpay_cn:国际银联 alipay_hk:香港支付宝 bitcoin:b特币 usdt 钱包
'amount' => $data['create_amount'], //付款金额
'currency' => $data['currency'], //货币 CNY 人民币
'description' => 'pay', //付款描述
'order_id' => $data['order_id'], //商户订单号,最长64字符。默认不做唯一性限制。
'api_sig' => md5(implode('|', array($this->api_key, $data['pm_id'], $data['create_amount'], $data['currency'], $data['order_id'], $this->secret_Key))),
'return_url' => $this->return_url,
],
'verify' => false
]
)->getBody()->getContents();
$response = json_decode($response, true);
var_dump($response);
if ($response['result_code'] == 200) {
$this->toPayUrl = $response['redirect_url']; //付款链接Url
return true;
} else {
return false;
}
} catch (\Throwable $throwable) {
return $throwable->getMessage();
// return false;
}
}