Backend Development 6 min read

Using PHP cURL for GET, POST, File Upload, and General HTTP Requests

This guide shows how to enable the PHP cURL extension and provides complete code examples for performing HTTP GET, POST, file upload, and flexible request operations using cURL functions in PHP.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using PHP cURL for GET, POST, File Upload, and General HTTP Requests

The article explains that cURL is a PHP extension that must be enabled in the php.ini file and demonstrates how to use it for various HTTP operations.

GET method

function http_get($url){
  // 初始化
  $ch = curl_init();
  // 相关设置
  # 设置请求的URL地址
  curl_setopt($ch, CURLOPT_URL, $url);
  # 请求头关闭
  curl_setopt($ch, CURLOPT_HEADER, 0);
  # 请求的得到的结果不直接输出,而是以字符串结果返回  必写
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  # 设置请求的超时时间 单位秒
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  # 设置浏览器型号
  curl_setopt($ch, CURLOPT_USERAGENT, 'MSIE001');

  # 证书不检查
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

  // 发起请求
  $data = curl_exec($ch);

  // 有没有发生异常
  if (curl_errno($ch) > 0) {
    // 把错误发送给客户端
    echo curl_error($ch);
    $data = '';
  }

  // 关闭请求
  curl_close($ch);

  return $data;
}
echo http_get('http://localhost:8080/post.php');

POST method

function http_post($url, $ret){
  // 初始化
  $ch = curl_init();
  // 相关设置
  # 设置请求的URL地址
  curl_setopt($ch, CURLOPT_URL, $url);
  # 请求头关闭
  curl_setopt($ch, CURLOPT_HEADER, 0);
  # 请求的得到的结果不直接输出,而是以字符串结果返回  必写
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  # 设置请求的超时时间 单位秒
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  # 设置浏览器型号
  curl_setopt($ch, CURLOPT_USERAGENT, 'MSIE001');

  # 证书不检查
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

  # 设置为post请求
  curl_setopt($ch, CURLOPT_POST, 1);
  # post请求的数据
  curl_setopt($ch, CURLOPT_POSTFIELDS, $ret);

  // 发起请求
  $data = curl_exec($ch);

  // 有没有发生异常
  if (curl_errno($ch) > 0) {
    echo curl_error($ch);
    $data = '';
  }

  // 关闭请求
  curl_close($ch);

  return $data;
}
echo http_post('http://localhost:8080/post.php', ['id'=>1, 'name'=>'张三']);

File upload method

function http_post_file($url, $ret, $file=''){
  if (!empty($file)) {
    // 有文件上传
    $ret['pic'] = new CURLFile($file);
  }
  // 初始化
  $ch = curl_init();
  // 相关设置
  # 设置请求的URL地址
  curl_setopt($ch, CURLOPT_URL, $url);
  # 请求头关闭
  curl_setopt($ch, CURLOPT_HEADER, 0);
  # 请求的得到的结果不直接输出,而是以字符串结果返回  必写
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  # 设置请求的超时时间 单位秒
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  # 设置浏览器型号
  curl_setopt($ch, CURLOPT_USERAGENT, 'MSIE001');
  # 证书不检查
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  # 设置为post请求
  curl_setopt($ch, CURLOPT_POST, 1);
  # post请求的数据
  curl_setopt($ch, CURLOPT_POSTFIELDS, $ret);

  // 发起请求
  $data = curl_exec($ch);

  // 有没有发生异常
  if (curl_errno($ch) > 0) {
    echo curl_error($ch);
    $data = '';
  }

  // 关闭请求
  curl_close($ch);

  return $data;
}
$file = __DIR__ . '/1.jpg';
echo http_post_file('http://localhost:8080/post.php', ['id'=>1, 'name'=>'张三'], $file);

General request method

function http_request($url, $ret='', $file=''){
  if (!empty($file)) {
    $ret['pic'] = new CURLFile($file);
  }
  // 初始化
  $ch = curl_init();
  // 相关设置
  # 设置请求的URL地址
  curl_setopt($ch, CURLOPT_URL, $url);
  # 请求头关闭
  curl_setopt($ch, CURLOPT_HEADER, 0);
  # 请求的得到的结果不直接输出,而是以字符串结果返回  必写
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  # 设置请求的超时时间 单位秒
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  # 设置浏览器型号
  curl_setopt($ch, CURLOPT_USERAGENT, 'MSIE001');
  # 证书不检查
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  # 设置为post请求
  if ($ret) { // 如果 $ret 不为假则是 post 提交
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $ret);
  }
  // 发起请求
  $data = curl_exec($ch);

  // 有没有发生异常
  if (curl_errno($ch) > 0) {
    echo curl_error($ch);
    $data = '';
  }

  // 关闭请求
  curl_close($ch);

  return $data;
}
// GET example
echo http_request('https://wx.520xxxx.cn');

At the end, the article encourages readers to like and share the content as a form of support.

backendFile UploadhttpPHPcurlGETPOST
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.