封装了ThinkPHP用于发送GET和POST请求的公共方法。这个方法可以放在你的公共函数文件中,或者创建一个独立的类来管理这些请求。
<?php
namespace app\common\utils;use think\facade\Log;
use think\exception\HttpException;class HttpRequest
{/*** 发送GET请求** @param string $url 请求的URL* @param array $params URL参数* @param array $headers 请求头* @return mixed*/public static function get($url, $params = [], $headers = []){$ch = curl_init();if (!empty($params)) {$url .= '?' . http_build_query($params);}curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_TIMEOUT, 30);if (!empty($headers)) {curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);}$output = curl_exec($ch);$error = curl_error($ch);curl_close($ch);if ($error) {Log::error('HTTP GET Request Error: ' . $error);throw new HttpException(500, 'HTTP GET Request Error: ' . $error);}return json_decode($output, true);}/*** 发送POST请求** @param string $url 请求的URL* @param array $data POST数据* @param array $headers 请求头* @return mixed*/public static function post($url, $data = [], $headers = []){$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_TIMEOUT, 30);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));if (!empty($headers)) {curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);}$output = curl_exec($ch);$error = curl_error($ch);curl_close($ch);if ($error) {Log::error('HTTP POST Request Error: ' . $error);throw new HttpException(500, 'HTTP POST Request Error: ' . $error);}return json_decode($output, true);}
}
使用示例
你可以在你的控制器或模型中使用这个封装的公共方法来发送GET和POST请求。例如:
use app\common\utils\HttpRequest;class SomeController extends Controller
{public function someAction(){// 发送GET请求$getResponse = HttpRequest::get('https://api.example.com/data', ['param1' => 'value1']);// 发送POST请求$postResponse = HttpRequest::post('https://api.example.com/submit', ['field1' => 'value1']);// 处理响应if ($getResponse && $postResponse) {// 处理成功响应} else {// 处理错误}}
}
注意事项
- 错误处理:在生产环境中,确保有适当的错误处理机制。
- 超时设置:根据实际需求调整超时时间。
- 安全性:发送请求时注意数据的安全性,尤其是敏感信息的传输。
如果遇到报错:HTTP GET Request Error: SSL certificate problem: unable to get local issuer certificate 。说明ssl证书没配置,我这里懒得配置,你可以自行百度,所以我就干脆禁用检查ssl证书,当然不建议各位这样做!
<?php
namespace app\common\utils;use think\facade\Log;
use think\exception\HttpException;class HttpRequest
{/*** 发送GET请求** @param string $url 请求的URL* @param array $params URL参数* @param array $headers 请求头* @return mixed*/public static function get($url, $params = [], $headers = []){$ch = curl_init();if (!empty($params)) {$url .= '?' . http_build_query($params);}curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_TIMEOUT, 30);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用SSL验证curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 禁用SSL验证if (!empty($headers)) {curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);}$output = curl_exec($ch);$error = curl_error($ch);curl_close($ch);if ($error) {Log::error('HTTP GET Request Error: ' . $error);throw new HttpException(500, 'HTTP GET Request Error: ' . $error);}return json_decode($output, true);}/*** 发送POST请求** @param string $url 请求的URL* @param array $data POST数据* @param array $headers 请求头* @return mixed*/public static function post($url, $data = [], $headers = []){$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_TIMEOUT, 30);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用SSL验证curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 禁用SSL验证if (!empty($headers)) {curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);}$output = curl_exec($ch);$error = curl_error($ch);curl_close($ch);if ($error) {Log::error('HTTP POST Request Error: ' . $error);throw new HttpException(500, 'HTTP POST Request Error: ' . $error);}return json_decode($output, true);}
}