如何在json中的multipart中发布图像以卷曲发布请求

时间:2016-11-24 04:19:11

标签: php json curl

我想用卷曲的json post传递图像。 下面是我用过的代码

   $user_name = $_POST['user_name'];
            $first_name = $_POST['first_name'];
            $last_name = $_POST['last_name'];
            $country_code = $_POST['country_code'];
            $mobile_no = $_POST['mobile_no'];
            $email = $_POST['email'];
            $password = $_POST['password'];
            $tmpfile = $_FILES['profile_pic']['tmp_name'];
            $type =  $_FILES['profile_pic']['type'];
            $filename = $_FILES['profile_pic']['name'];

           $cfile = new CURLFile($filename,$type,'test_name');

            //print_r($cfile);exit;
            $data = array("method_name" => "sign_up_new", 
                            "body"=>array("user_name"=>$user_name,"email"=>$email,"country_code"=>$country_code,
                                            "mobile_no"=>$mobile_no,"first_name"=>$first_name,
                                            "last_name"=>$last_name,"password"=>$password,
                                            "profile_pic"=>$cfile));

$data_string = json_encode($data);
    $data_string=array("json"=>$data_string);
    $ch = curl_init(apiUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    $result = curl_exec($ch);
    curl_close($ch);
print_r($result);

当我打印

  

$ _文件

在请求的文件中没有打印, 你能帮我解决这个问题吗? 提前谢谢。

0 个答案:

没有答案