php各个版本curl上传文件的兼容实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 以POST方式提交数据
   public function post_data($url, $param, $is_file = false, $return_array = true) {
      ........
       $ch = curl_init ();
       curl_setopt ( $ch, CURLOPT_URL, $url );
       curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
       curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
       curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
       curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
       curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)' );
       curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
       curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
 
       curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
       if($is_file){
           if(version_compare(phpversion(),'5.6')>=0){
               if(version_compare(phpversion(),'7')>=0){
                   $param['media'] = new \CURLFile(ltrim($param['media'], '@'));
               }else{
                   curl_setopt ( $ch, CURLOPT_SAFE_UPLOAD, false);
               }
           }
       }
       curl_setopt ( $ch, CURLOPT_POSTFIELDS, $param );
 
       $res = curl_exec ( $ch );
 
       curl_close ( $ch );
 
      .......
 
       return $res;
   }

  

posted @   兔六哥  阅读(383)  评论(0编辑  收藏  举报
编辑推荐:
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
阅读排行:
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能
· 语音处理 开源项目 EchoSharp
· drools 规则引擎和 solon-flow 哪个好?solon-flow 简明教程
点击右上角即可分享
微信分享提示