Thread: Post with cURL

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    [Resolved] Post with cURL


    Hello,

    I'm trying to post a file from the local server to a remote server. I'm using cURL to do so, but it's not working right.

    PHP Code:
    $filename '@'.getcwd()."/test.txt";
    $vars[0] = "file=".urlencode($filename);
    $vars[1] = "newName=".urlencode("success.txt");
    $postString implode('&'$getVars);

    $url "www.myserver.com/postProcess.php";

    $ch curl_init();
    curl_setopt($ch,CURLOPT_URL$url);
    curl_setopt($ch,CURLOPT_POSTcount($vars));
    curl_setopt($ch,CURLOPT_POSTFIELDS$poststring);
    curl_setopt($ch,CURLOPT_RETURNTRANSFERtrue);
    $result curl_exec($ch);
    curl_close($ch); 
    From everything I've read, this ('@'.getcwd()."/test.txt") should be pulling the file and posting it to the external server.

    a print_r($_POST) just gives me these results though...
    Code:
    Array ( [file] => @/var/www/html/test.txt [newName] => success.txt )
    The actual file itself is not being sent, just the path

    Thanks to anyone who can help me out.
    Last edited by jiceman; November 5th, 2012 at 08:07 AM. Reason: problem solved
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    From everything I've read, this ('@'.getcwd()."/test.txt") should be pulling the file and posting it to the external server.
    Where did you read that? It's not true. Use file_get_contents to get a file's contents.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Thank you Dan.
    I read that the "@" symbol was supposed to tell cURL that what followed was a reference to a file on the local system, and to send that file during the post.

    I'm trying to send a pdf file. The pdf file is 2 pages, and now I'm creating a blank 2 page pdf file.
    I'm trying 2 different methods, but I get the same results of just a blank 2 page pdf.

    PHP Code:
    $handle fopen("files/test.pdf"'a');
    fwrite($handle$_POST['file']);
    fclose($handle);

    file_put_contents("files/test2.pdf"$_POST['file']); 
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Oh man, I totally brain farted on @. Yes, that's correct.

    This code should work:

    $data['Filedata'] = "@".$file_path;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $response = curl_exec($ch);
    return $response;


    POSTFIELDS needs to have a variable called 'Filedata'. you can't just stick it into the array.

    And once it makes it to the posted script, it will be an actual file upload, not a regular POST field.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Dan, You're awesome. You are great help.
    One last thing...

    Is it possible to send the file like that and other variables as well, particularly an array.

    So for example, what I was sending before (being unable to send the file) was something like this...
    PHP Code:
    $vars['names'][] = "a";
    $vars['names'][] = "b";
    $vars['names'][] = "c";
    $vars['names'][] = "d";
    $vars['names'][] = "e";
    $vars['file'] = "@/var/www/html/file.pdf"
    I then turned that into a GET type string. (like this: "var=value&var2=value2&var3=value3")
    On the target server, the $_POST variable would recreate the arrays as desired.
    the only issue was that the file wouldn't get sent.

    So now the file is being sent, but if my array is something like...
    PHP Code:
    $vars['test'] = "test value";
    $vars['names'][] = "a";
    $vars['names'][] = "b";
    $vars['names'][] = "c";
    $vars['names'][] = "d";
    $vars['names'][] = "e";
    $vars['Filedata'] = "@/var/www/html/file.pdf"
    And just the $vars is being posted in the POSTFIELDS instead of the string form,
    Then the file does get posted like I wanted, but now the $_POST['names'] just equals the string "Array".
    $_POST['test'] does equal "test value" though.

    Is it possible to send the file, and data arrays, so that $_POST will recreate the arrays like it did before?
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    Yes, something like this should work:
    PHP Code:
      $vars['names[0]'] = "a";
    $vars['names[1]'] = "b";
    $vars['names[2]'] = "c";
    $vars['names[3]'] = "d";
    $vars['names[4]'] = "e";
    $vars['file'] = "@/var/www/html/file.pdf"

    Comments on this post

    • ManiacDan agrees
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    That makes sense now that I see it. Thank you E-Oreo.
    It works perfectly.
    Thanks again ManiacDan.

IMN logo majestic logo threadwatch logo seochat tools logo