• curl (post/get)请求,文件上传


    1,post请求

    示例1:

     1   public function request($requestUrl,$paramenters){  //传入请求连接url 和参数        参数为json 编码
     2              //$access_token='NGFkMWYwOTc1NTdkNGRhYWE4ZjlmM2QyODNmYTJlYmM';
     3              $access_token=$this->access_token_verify();
     4   
     5              $headers = array();  //设置请求头文件
     6              $headers[]='Authorization: Bearer '.$access_token;  
     7              $headers[]='Content-Type: application/json';
     8              //dump($headers);
     9              $url =$requestUrl;
    10              $curl = curl_init();
    11              curl_setopt($curl, CURLOPT_URL, $url);
    12              curl_setopt($curl, CURLOPT_HTTPHEADER,$headers); //请求头信息
    13              curl_setopt($curl, CURLOPT_HEADER,0);
    14              curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    15              curl_setopt($curl, CURLOPT_POST, true); //post传输数据
    16              curl_setopt($curl, CURLOPT_POSTFIELDS,$paramenters);
    17  
    18              //curl_setopt($curl,CURLINFO_HEADER_OUT,true);  //收集请求头信息  curl设置一个信息一个信息才有效 不设置是不起作用的,这里为了看自定义的请求头文件是否正确
    19  
    20              $data = curl_exec($curl);
    21  
    22              //dump(curl_getinfo($curl,CURLINFO_HEADER_OUT));  //打印请求头信息
    23  
    24              curl_close($curl);
    25              dump($data);  //打印请求数据     
    26              return $data;
    27          }

     示例2:

     1  public function request($requesturl,$paramenters,$signature){  //参数为数组
     2         //$paramenters['sign_type']='CERT';
     3         //$paramenters['sign']=$signature; //签名类型和签名字符串
     4         $url =$requesturl;
     5         $curl = curl_init();
     6         curl_setopt($curl, CURLOPT_URL, $url);
     7         curl_setopt($curl, CURLOPT_HEADER,0);
     8         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     9         curl_setopt($curl, CURLOPT_POST, true);
    10         curl_setopt($curl, CURLOPT_POSTFIELDS,http_build_query($paramenters));  //http_build_query根据数组产生一个urlencode之后的请求字符串
    11         $data = curl_exec($curl);
    12         curl_close($curl);
    13         return $data;
    14 
    15     }

    2,get请求

     1 public function request($requestUrl,$idNo){  //传入请求链接和参数
     2 
     3             $access_token=$this->access_token_verify();
     4 
     5             //dump($requestUrl);
     6            // dump($idNo);
     7             $url =$requestUrl.$idNo;//dump($url);
     8 
     9             $headers = array();
    10             $headers[]='Authorization: Bearer '.$access_token;
    11             $curl = curl_init();
    12             curl_setopt ($curl, CURLOPT_URL, $url);
    13             curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
    14             curl_setopt($curl, CURLOPT_HEADER,0);
    15             curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
    16         
    17 
    18             //curl_setopt($curl,CURLINFO_HEADER_OUT,true);
    19 
    20             $status_data = curl_exec($curl);
    21 
    22             //dump(curl_getinfo($curl,CURLINFO_HEADER_OUT));
    23 
    24             curl_close($curl);
    25             // dump($status_data);
    26             return $status_data;
    27         }

    3, 上传 .zip 格式文件

     1 public function upload_zip(){
     2           //dump($_POST);
     3             /*dump($_FILES);
     4             dump($_FILES['filew']['name']);*///die;
     5             $requestUrl=self::BASE_URL.self::UPLOAD_ZIP;  //dump($requestUrl);                
     6                 $post_data=array(   
     7                         "extId"=>'YW005-170413-001',        
     8                         "file" => '@'.ROOT_PATH."public/static/aaa.zip"  //文件要以@开头
     9                     );                        
    10                 $access_token=$this->access_token_verify();//die;
    11 
    12                 $headers = array();
    13                 $headers[]='Content-Type: multipart/form-data';
    14                 $headers[]='Authorization: Bearer '.$access_token;
    15 
    16                 $url =$requestUrl;//dump($url);//die;
    17                 $curl = curl_init();
    18                 curl_setopt($curl, CURLOPT_URL, $url);
    19                 curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
    20                 curl_setopt($curl, CURLOPT_HEADER,0);
    21                 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    22                 curl_setopt($curl, CURLOPT_POST,1);
    23                 curl_setopt($curl, CURLOPT_POSTFIELDS,$post_data);
    24 
    25                 //curl_setopt($curl,CURLINFO_HEADER_OUT,true);
    26                 $data = curl_exec($curl);
    27                 //dump(curl_getinfo($curl,CURLINFO_HEADER_OUT));
    28                 //var_dump(curl_error($curl));//die;
    29                 curl_close($curl);
    30                // dump($data);       
    31                 return $data;
    32         }

     4,出现错误: curl  SSL certificate problem: unable to get local issuer certificate

      如果不紧要的可以选择跳过ssl的检测  ,  在curl_exec之前

      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  • 相关阅读:
    Yii2 使用 Beanstalk 队列系统
    Yii2 注册表单验证规则 手机注册时候使用短信验证码
    Yii2 高级模板 多域名管理问题
    PHP生成缩略图,控制图片质量,支持.png .jpg .gif
    yii2-lock-form 也许这就是你想要的,阻止表单多次提交
    PHP日期与时间戳转换
    PHP/Yii2操作Cookie,常见问题以及注意事项
    对称加密,API加密
    yii2弹出层
    两种不同的Context
  • 原文地址:https://www.cnblogs.com/ikoala/p/7448541.html
Copyright © 2020-2023  润新知