Top Questions

  • 2.9 Years

    • Category : Php
    • Location : Chandigarh
    • Posted On : Sep 15, 2017

    Using this code you can upload and image using curl with third party role

    
       /*Send Page*/
    
       $filename = covertImage($filename);
       
       $fields = array(
                            'customer_name' => 'Gulshan Sharma',
                            'filename' => $filename,
    
                    );
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL,'http:example.com/upload/');
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_USERAGENT, "webapp");
                    //curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
    
                    // in real life you should use something like:
                    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
                    // receive server response ...
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
                    curl_setopt($ch, CURLOPT_USERPWD, "xxx:xxx"); 
                    $server_output = curl_exec ($ch);
                    curl_close ($ch);
    
      function covertImage($path){
    
                $type = pathinfo($path, PATHINFO_EXTENSION);
                $data = file_get_contents($path);
                $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
                return $base64;
       }
     /*RecivePage*/
    
       $filename = base64_to_jpeg($_POST['filename']);
       
       echo $filename;
       
       function base64_to_jpeg($base64_string) {
                         $upload_path = '/hosted-libraries/temp/';
                        $upload_path = $_SERVER['DOCUMENT_ROOT'] . $upload_path;
                        $newImg = uniqid();
                        $img = str_replace('data:image/png;base64,', '', $base64_string);
                        $img = str_replace(' ', ' ', $img);
                        $data = base64_decode($img);
                        $file = $upload_path . $newImg . '.png';
                        $success = file_put_contents($file, $data);
                        if($success)
                            return $file;
                        else
                            FALSE;
                       
         }

Add Your Answer

When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.