Multipart form file upload POST PHP cURL

前端 未结 4 590
慢半拍i
慢半拍i 2021-01-15 02:30

I am wanting to send a file via http POST using PHP and cURL.

The form POST was working ok with basic fields besides the file being posted with \'application/json\'.

4条回答
  •  天命终不由人
    2021-01-15 02:58

    This all gets very confusing and "contradictory" very quickly! Because cUrl is quite flexible and powerful, little differences in usage context have a massive impact and can lead to DAYS of non-existent bug chasing.

    Secondly, the URL/Endpoint you are posting to can also have it own "implementation" and expectations. This understanding it complicated when experience is only with GET requests AND the assumption/expectation is based of PHP Super Easy $_REQUEST

    In the case above, simplifying: This is a live working example, but the postfields ($post) has been reduced from the 50 odd in use to a few for example. This will post as multipart form data.

    Here important options are the options to use CURLOPT_POST and CURLOPT_POSTFIELDS and NOT CURLOPT_CUSTOMREQUEST. Doing so will require you to take care of required headers and in particular, content size header, etc.

    // Post "field" array. Notice its 1 Dimensional. Else, this should rather accept JSON. simply decode array to json and proceed). Depends what server is expecting - Form data, Form + files, json or maybe just a RAW request body.
    $post = array(  
                'RsmMaster1_TSM' => $tsm,
                '__EVENTTARGET' => '__Page',
                '__EVENTARGUMENT' => 'ExcelExport',
                '__VIEWSTATE'  => $viewstate,
                '__VIEWSTATEGENERATOR'  => $viewstategenerator,
                '__EVENTVALIDATION'  => $eventvalidation
     );
    
    // Optional, Required in THIS case because, well gosh darn who knows, as the receiving server admin?
    $headers = array(   
                    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                    'Accept-Language: en-ZA,en-GB;q=0.8,en-US;q=0.5,en;q=0.3',
                    'Referer: https://example.com/Main.aspx'  
                );
    
    // cUrl it into the goal!
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'https://example.com/Main.aspx');
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');                                                                   
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);    
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); // Session maintain!
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_FAILONERROR,    TRUE   );
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE  );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE  );
    
    $content = curl_exec($ch);
    curl_close($ch);
    

提交回复
热议问题