CURL POST上传文件

<?php
/**
 * Note:for multipart/form-data upload
 * 这个是标准表单上传PHP文件
 * Please be amended accordingly based on the actual situation
 */
if (!$_FILES['Filedata']) {
   die ( 'Image data not detected!' );
}
if ($_FILES['Filedata']['error'] > 0) {
   switch ($_FILES ['Filedata'] ['error']) {
      case 1 :
         $error_log = 'The file is bigger than this PHP installation allows';
         break;
      case 2 :
         $error_log = 'The file is bigger than this form allows';
         break;
      case 3 :
         $error_log = 'Only part of the file was uploaded';
         break;
      case 4 :
         $error_log = 'No file was uploaded';
         break;
      default :
         break;
   }
   die ( 'upload error:' . $error_log );
} else {
   $img_data = $_FILES['Filedata']['tmp_name'];
   $size = getimagesize($img_data);
   $file_type = $size['mime'];
   if (!in_array($file_type, array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'))) {
      $error_log = 'only allow jpg,png,gif';
      die ( 'upload error:' . $error_log );
   }
   switch($file_type) {
      case 'image/jpg' :
      case 'image/jpeg' :
      case 'image/pjpeg' :
         $extension = 'jpg';
         break;
      case 'image/png' :
         $extension = 'png';
         break;
      case 'image/gif' :
         $extension = 'gif';
         break;
   }  
}
if (!is_file($img_data)) {
   die ( 'Image upload error!' );
}
//图片保存路径,默认保存在该代码所在目录(可根据实际需求修改保存路径)
$save_path = dirname( __FILE__ );
$uinqid = uniqid();
$filename = $save_path . '/' . $uinqid . '.' . $extension;
$result = move_uploaded_file( $img_data, $filename );
if ( ! $result || ! is_file( $filename ) ) {
   die ( 'Image upload error!' );
}



echo 'Image data save successed,file:' . $filename;


$ch = curl_init();

$data = array('userId' => 45666,'param' => "exam_explain_4_text",'target' => 96, 'file' => new \CURLFile(realpath("$filename")));

curl_setopt($ch, CURLOPT_URL, 'http://xxxxxxxxxxxxx');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);


$response = curl_exec($ch);
$err = curl_error($ch);

curl_close($ch);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

以上为原生实现

 

ws-http实现:

https://github.com/toohamster/ws-http

<?php
/**
 * Created by 小雨在线.
 * User: 飛天
 * Date: 2017/8/8 0008
 * Time: 11:22
 */



require 'vendor/autoload.php';



$httpRequest = \Ws\Http\Request::create();


$headers = array('Accept' => 'application/json');


$data = array('userId' => 45666,'param' => "exam_explain_4_text",'target' => 96);





$files = array('file' => './logo.jpg');



$body = Ws\Http\Request\Body::multipart($data, $files);



$response = $httpRequest->post('http://xxxxxxxxx', $headers, $body);

 

unirest实现

https://github.com/Mashape/unirest-php

$headers = array('Accept' => 'application/json');


$data = array('userId' => 45666,'param' => "exam_explain_4_text",'target' => 96);


$files = array('file' => './logo.jpg');



$body = Unirest\Request\Body::multipart($data, $files);

$response = Unirest\Request::post('http://xxxxxxxxxx', $headers, $body);

 

 

转载于:https://my.oschina.net/yjft/blog/1504493

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值