怎么在PHP使用curl请求实现post上传图片

发布时间:2021-04-07 17:27:13 作者:Leah
来源:亿速云 阅读:209

怎么在PHP使用curl请求实现post上传图片?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

php代码:

/* 使用curl函数 */
$url = "https://cache.yisu.com/upload/information/20201209/266/37581',
);
$response = curl_http($url, 'POST', $post_data);
$params = array();
$params = json_decode($response,true);
if (isset($params['errcode']))
{
  echo "error:" . $params['errcode'];
  echo "msg :" . $params['errmsg'];
  exit;
}
var_dump( $params );
/**
 * http请求方式: 默认GET
 */
function curl_http($url, $method="GET", $postfields){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_URL, $url);
  switch ($method) {
    case "POST":
      curl_setopt($ch, CURLOPT_POST, true);
      if (!empty($postfields)) {
        $hadFile = false;
        if (is_array($postfields) && isset($postfields['media'])) {
          /* 支持文件上传 */
          if (class_exists('\CURLFile')) {
            curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
            foreach ($postfields as $key => $value) {
              if (isPostHasFile($value)) {
                $postfields[$key] = new \CURLFile(realpath(ltrim($value, '@')));
                $hadFile = true;
              }
            }
          } elseif (defined('CURLOPT_SAFE_UPLOAD')) {
            if (isPostHasFile($value)) {
              curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
              $hadFile = true;
            }
          }
        }
        $tmpdatastr = (!$hadFile && is_array($postfields)) ? http_build_query($postfields) : $postfields;
        curl_setopt($ch, CURLOPT_POSTFIELDS, $tmpdatastr);
      }
      break;
    default:
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
      break;
  }
  $ssl = preg_match('/^https:\/\//i',$url) ? TRUE : FALSE;
  curl_setopt($ch, CURLOPT_URL, $url);
  if($ssl){
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
  }
  $response = curl_exec($ch);
  curl_close($ch);
  if(empty($response)){
    exit("错误请求");
  }
  return $response;
}
function isPostHasFile($value)
{
  if (is_string($value) && strpos($value, '@') === 0 && is_file(realpath(ltrim($value, '@')))) {
    return true;
  }
  return false;
}

也可以使用php内置的系统函数,如果使用过程中出现问题,建议查看是否启用相应的系统函数。

使用exec系统函数:

/* 使用exec函数 */
$command = 'curl -F media=@'.$filepath.' "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=image"';
$retval = array();
exec($command, $retval, $status);
$params = array();
$params = json_decode($retval[0],true);
if ($status != 0) {
  $params = array(
    'errcode'  => '-100',
    'errmsg'  => '公众号服务出错,请联系管理员',
  );
}
return $params;

使用system系统函数:

/* 使用system函数 */
$command = 'curl -F media=@'.$filepath.' "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=image"';
$retval = 1;
$last_line = system($command, $retval);
$params = array();
$params = json_decode($last_line,true);
if ($retval != 0) {
  if (isset($params['errcode'])) {
    $params = array(
      'errcode'  => '-100',
      'errmsg'  => '公众号服务出错,请联系管理员',
    );
  }
}
return $params;

看完上述内容,你们掌握怎么在PHP使用curl请求实现post上传图片的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. php模拟post提交请求,curl调用接口
  2. php 利用curl发送post请求

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php curl post

上一篇:怎么在Java中实现一个傅里叶变化算法

下一篇:使用Python怎么绘制双柱状图并显示数值

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》