使用php中的curl上传带有图像的表单数据

时间:2017-08-29 11:39:15

标签: php

我有API的网址。我的任务是将数据存储在表单数据库中。但是当使用curl时,它没有设置错误URL。请查看代码。

 $full_name=$_REQUEST['full_name'];
          $email=$_REQUEST['email'];
          $mobile_number=$_REQUEST['mobile_number'];
          $pass=$_REQUEST['pass'];
          $work_address=$_REQUEST['work_address'];
          $file0=$_FILES['doc1']['name'];
          $file1=$_FILES['doc2']['name'];
          $file2=$_FILES['doc3']['name'];

        $url="http://34.195.215.247/sudzero/api/washer-register.php?fname='" . $full_name . "'&email='" . $email . "'&pass='" . $pass . "'&address='" . $work_address . "'&phone='".$mobile_number."'&file0='".$file0."'&file1='".$file1."'&file2='".$file2."'"; echo $url;
        $curl    = curl_init(); 
          $headers = array("Content-Type:multipart/form-data");
            curl_setopt_array($curl, array(
            curlopt_returntransfer => 1,
            curlopt_url => $url,
            CURLOPT_HEADER => true,
            curlopt_useragent => 'cubewires sample curl request',
            curlopt_post => 1,
            CURLOPT_HTTPHEADER => $headers,
            curlopt_postfields => array(
                'fname' => $full_name,
                'email' => $email,
                'pass' => $pass,
                'address' => $work_address,
                'phone' => $mobile_number,
                'file0' => $file0,
                'file1' => $file1,
                'file2' => $file2

            )
        ));

        $resp = curl_exec($curl);

        if (curl_errno($curl)) {

        $msg = curl_error($curl);
            }
            else {

                $msg = 'data uploaded successfully.';
            }
        curl_close($curl);
        $return = array('msg' => $msg);

        echo json_encode($return);

请帮帮我朋友。我失去了两个小时。 感谢

2 个答案:

答案 0 :(得分:1)

大写CURL选项

curlopt_url => $url,

//change the above to 
CURLOPT_URL => $url,

也是以下字段

CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERAGENT => 'cubewires sample curl request',
CURLOPT_POST => 1,

答案 1 :(得分:-1)

$ file0 = $ _FILES ['doc1'] ['tmp_name'];

$文件1 = $ _ FILES [ 'DOC2'] [ 'tmp_name的值'];

$文件2 = $ _ FILES [ 'doc3的'] [ 'tmp_name的值'];