0

I have URL of an API. my task is stored data in database of form. but when work with curl it's throw error URL not set. Please have look of code.

 $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);

Please help me friends. i have lost my two hours. Thanks

Vivek Mishra
  • 37
  • 1
  • 7

2 Answers2

1

Capitalize the CURL options

curlopt_url => $url,

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

also the following fields too

CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERAGENT => 'cubewires sample curl request',
CURLOPT_POST => 1,
nithinTa
  • 1,632
  • 2
  • 16
  • 32
  • but data does not insert in database. – Vivek Mishra Aug 29 '17 at 12:00
  • try $file0 = curl_file_create( $_FILES['doc1']['tmp_name'] ); are you getting a temporary name with $_FILES['doc1']['tmp_name'] ? – nithinTa Aug 29 '17 at 12:06
  • When i fix curl_file_create than again error is No URL set! – Vivek Mishra Aug 29 '17 at 12:13
  • yeah i am getting tmp_name with file – Vivek Mishra Aug 29 '17 at 12:14
  • when i print_r response than i got this data. – Vivek Mishra Aug 29 '17 at 12:22
  • HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Tue, 29 Aug 2017 12:20:01 GMT Server: Apache/2.4.23 (Amazon) PHP/5.6.28 X-Powered-By: PHP/5.6.28 Content-Length: 14 Content-Type: text/html; charset=UTF-8 – Vivek Mishra Aug 29 '17 at 12:22
  • With these two changes i'm getting uploaded file with your code. 1)changed options to capital case. 2) $file0 = curl_file_create($_FILES['doc1']['tmp_name']); and i'm receiving my uploaded file. Alternatively you can pass $url in curl_init($url). Also curl_create_file requires php 5.5+ Please turn on your error reporting to see what happens. Can you please print what your variable $url holds ? https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php – nithinTa Aug 29 '17 at 12:47
  • where are you define curl_file_create($_FILES['doc1']['tmp_name']);. I define this function in postfields. – Vivek Mishra Aug 29 '17 at 12:54
  • if response working well but response i getting from there i mentioned above. my problem is data is not inserted in database and file also not upload in directory – Vivek Mishra Aug 29 '17 at 13:05
-1

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

$file1=$_FILES['doc2']['tmp_name'];

$file2=$_FILES['doc3']['tmp_name'];