1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

using curl to reply to multipart forms

Discussion in 'PHP' started by dor@tehexploa, May 27, 2009.

  1. #1
    the closest thing to help i've found encountering this issue is:
    http://forums.digitalpoint.com/showthread.php?t=1154563&highlight=multipart+form

    i'm kinda new to php but i've been able to do a good amount of curl regarding httpS sites. now i'm trying to post to a rather large form that incorporates multiparts.

    in the thread above he mentions that switching the variable $postdata to an array.. i'm not exactly sure how this would look. i thought it was just name => value , but when you add 'Content-Disposition's in the mix I'm kinda at a lost where or how i would make it look.

    And do you basically just recreate EXACTLY how it looks in say Live HTTP Headers (the prog I use to capture headers)..
    ex:
    "Content-Disposition: form-data; name=\"title\"\r\n\r\n";
    "Content-Disposition: form-data; name=\"bodytext\"\r\n\r\n";
    "Content-Disposition: form-data; name=\"title\"\r\n\r\n";
    ... and so on..

    Here is the code I'm referring to from the previous thread:
    $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):
     
    dor@tehexploa, May 27, 2009 IP
  2. octalsystems

    octalsystems Well-Known Member

    Messages:
    352
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    135
    Digital Goods:
    1
    #2
    [/CODE][/QUOTE]


    just use this
    $postdata="title=somevalue&body=somevalue";
    $ch = curl_init($posturl);
    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);
     
    octalsystems, May 28, 2009 IP
  3. dor@tehexploa

    dor@tehexploa Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    HMmM i'm prolly wrong, but i thought if you were dealing with Multipart forms the $postdata had to be passed as an array
    i was thinking it might be like
    $postdata = array("title"=> "somevalue"; "body"=> "somevalue"; .....);

    here is why i think that
    this comes from http://us3.php.net/manual/en/function.curl-setopt.php
    To make a POST in multipart/form-data mode
    this worked for me, the " \n" at the end of the variables was very important on my OS X server.
    
    <?php
    
    $file = "file_to_upload.txt";
    $submit_url = "http://www.example.com/upload_page.php";
    
    $formvars = array("cc"=>"us \n");
    $formvars[variable_1] = "bla bla \n";
    $formvars[variable_2] = "bla bla \n";
    $formvars[variable_3] = "bla bla \n";
    $formvars[variable_4] = "bla bla \n";
    $formvars[upfile] = "@$file"; // "@" causes cURL to send as file and not string (I believe)
    
        // init curl handle
        $ch = curl_init($submit_url);
        curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");  //initiates cookie file if needed
        curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");  // Uses cookies from previous session if exist
        curl_setopt($ch, CURLOPT_REFERER, "http://www.example.net");  //if server needs to think this post came from elsewhere
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // follow redirects recursively
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $formvars);
    
        // perform post
        echo $pnp_result_page = curl_exec($ch);
        curl_close ($ch);
    
    ?>
    PHP:
    And now that I think about what this post says.. Will different servers require different formats of the postdata? Or is this all generated for you per server depending on the contents of your array?

    To be honest.. I haven't read if this is against the rules, but what I'm trying to do is post to Craigslist using a PHP script. This might makes things clearer as to what I'm intending or what server I'm targeting. Thanks for the reply, octalsystems. I'm prolly talking out my ass. I'm pretty new to PHP, so be gentle if you happen to reply again and I'm way wrong. :-p
     
    dor@tehexploa, May 28, 2009 IP
  4. octalsystems

    octalsystems Well-Known Member

    Messages:
    352
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    135
    Digital Goods:
    1
    #4
    both are right either name value pair or array with curl
     
    octalsystems, May 28, 2009 IP
  5. TecBrat

    TecBrat Member

    Messages:
    31
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #5
    I'll be honest, I was a little lazy and didn't read your whole post, but here's a link to something I wrote about this on my site:
    link

    It might be helpful.
     
    TecBrat, May 28, 2009 IP