Curl and multipart/form-data, can anyone help?

Discussion in 'PHP' started by smilesquare, Dec 17, 2008.

  1. #1
    can anyone help to correct my script to Post multipart/form-data

    Thanks.

    $srand = substr(md5(rand(0,32000)),0,10);
    $aform =      "----$srand\r\n";
    
    $submitdata = $aform;
    $submitdata .= "Content-Disposition: form-data; name=\"title\"\r\n\r\n";
    $submitdata .= "$title";
    $submitdata .= "\r\n".$aform;
    $submitdata .= "Content-Disposition: form-data; name=\"bodytext\"\r\n\r\n";
    $submitdata .= "$body";
    $submitdata .= "\r\n"."----$srand"."--\r\n";
    
    $postdata = $submitdata;
    $posturl = "http://test.com/submit.php";
     $ch = curl_init($posturl);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "Content-Type: multipart/form-data; boundary=--$srand");
     curl_setopt($ch, CURLOPT_POST      ,1);
     curl_setopt($ch, CURLOPT_POSTFIELDS    ,$postdata);
     curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,0);
     curl_setopt($ch, CURLOPT_COOKIE,  $mycookies);
     curl_setopt($ch, CURLOPT_HEADER      ,1);  // DO NOT RETURN HTTP HEADERS
     curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL
     $return = curl_exec($ch);
     curl_close($ch);
    Code (markup):
     
    smilesquare, Dec 17, 2008 IP
  2. smilesquare

    smilesquare Peon

    Messages:
    70
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Got it. Just pass postfields as an array.
     
    smilesquare, Dec 17, 2008 IP
  3. Internet Cowboy

    Internet Cowboy Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I've been searching the whole freaking internet on how to emulate multipart/form-data posting with Curl... and nothing. Finally, found this - a dead solo-post that, apparently, solved all problems. Thanks.
     
    Internet Cowboy, Apr 3, 2009 IP
  4. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    unfortunately it didn't solve it for me, is all the code above necessary to do a multipart/form-data post (but not posting a file)? i've gone over and over my code but it won't post the form-data at all.
     
    szalinski, Apr 22, 2009 IP
  5. TecBrat

    TecBrat Member

    Messages:
    31
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #5
    TecBrat, Feb 16, 2010 IP
  6. Izonedig

    Izonedig Member

    Messages:
    150
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #6
    Izonedig, Feb 16, 2010 IP