原生的cURL函数而不是 tp6框架的Http类,curl_init()、curl_setopt()和curl_exec()等cURL函数

GET请求示例:

// 初始化 cURL
$ch = curl_init();

// 设置 cURL 选项
curl_setopt($ch, CURLOPT_URL, 'https://example.com/api/resource');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// 执行 cURL 并获取返回结果
$response = curl_exec($ch);

// 关闭 cURL 资源
curl_close($ch);

// 处理返回结果
var_dump($response);

POST请求示例(带Body参数):

// 初始化 cURL
$ch = curl_init();

// 设置 cURL 选项
curl_setopt($ch, CURLOPT_URL, 'https://example.com/api/post-endpoint');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

// 设置POST数据
$postData = [
    'key'   => 'value',
    'param' => 'another value',
];
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

// 执行 cURL 并获取返回结果
$response = curl_exec($ch);

// 关闭 cURL 资源
curl_close($ch);

// 处理返回结果
var_dump($response);

POST请求示例(JSON格式):

$ch = curl_init('https://example.com/api/post-endpoint');

// 设置 cURL 选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

// 设置POST数据为JSON格式
$jsonData = [
    'key'   => 'value',
    'param' => 'another value',
];

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($jsonData));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);

// 执行 cURL 并获取返回结果
$response = curl_exec($ch);

// 关闭 cURL 资源
curl_close($ch);

// 处理返回结果
var_dump($response);
 

设置Header参数:

$ch = curl_init('https://example.com/api/resource');

// 设置Header $headers = [ 'Authorization: Bearer YourAccessToken', 'Content-Type: application/json', ];

$headers = ['Content-Type: multipart/form-data', ];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// 执行 cURL 并获取返回结果 $response = curl_exec($ch);

// 关闭 cURL 资源 curl_close($ch);

// 处理返回结果 var_dump($response);

上传文件示例:

$ch = curl_init('https://example.com/api/upload');

// 设置 cURL 选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

// 上传文件
$postData = [
    'file' => new \CURLFile('/path/to/your/file.jpg'),
];

$postData = [
 'file' => new \CURLFile($fileInfo->getPathname(), $fileInfo->getMime(), $fileInfo->getOriginalName()),
];


$headers = [
    'token: ' . $token,
  //"token:$token"
    'Content-Type: multipart/form-data',
];  //headers带token
curl_setopt($ch, CURLOPT_URL, self::$ApiUploadUrl);  // 设置请求的URL
curl_setopt($ch, CURLOPT_POST, 1);                   // 设置为POST请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData);    //设置POST数据
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);    // 设置HTTP请求头
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    // 设置是否将结果返回
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用SSL证书验证(慎用)
// 设置HTTP请求头 curl_setopt($ch, CURLOPT_HTTPHEADER, array( 
'Content-Type: application/json; charset=utf-8', 
'Accept: application/json', 
'Cache-Control: no-cache', 
'Pragma: no-cache' )
);

'Content-Type: application/json; charset=utf-8':意义:指定了请求体的内容类型为JSON,并且字符集为UTF-8。

Accept: application/json':意义:指定客户端期望接收的响应内容类型为JSON。

'Cache-Control: no-cache':意义:要求不要缓存该请求的响应。

'Pragma: no-cache':意义:与Cache-Control: no-cache 同样,要求不要缓存该请求的响应。

  • 解释:在一些老旧的系统中,Pragma 是 HTTP/1.0 的一个头部字段,而 Cache-Control 是 HTTP/1.1 的。两者都使用 no-cache 来达到不缓存的效果。

// 执行 cURL 并获取返回结果
$response = curl_exec($ch);

// 关闭 cURL 资源
curl_close($ch);

// 处理返回结果
var_dump($response);
 

你可能感兴趣的:(服务器,http,php)