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

    Join Date
    Dec 2006
    Posts
    21
    Rep Power
    0

    PHP cURL. Send a file


    Hi,

    I have been looking for information about this problem for the last two days and nothing has worked as I needed, that's why I am writing here now.

    I am working on connecting interfaces using XML messages and I have been always using cURL for this purpose with no problem. I just kept sending my xml message as a string inside a POST variable and always got the response with no much deal.

    Since last week I have been requested to join the interface of the company where I work to a new channel we will join to. After reading the documentation of this new interface, I thought everything would just work the same way but it is not like this.

    This new channel is indeed expecting to receive a xml FILE (not just a POST variable) and I have tried to send it in as many ways as I could think about and never worked. In fact, the only way I have gotten a real response from this channel was while using a <form> and a <input type="file" name="xmlfile"> and sending this file using the submit function of this form.

    The problem is that in my code there is no place for a form, while everything is running internally with php code, being launched each time an update is needed.

    I have been told that one call in the command line like this would work:

    Code:
    curl -F "xmlfile=@/test_2_send.xml" "http://url_where_to_send.html" > response_we_got.state.zip
    These are the different ways I have already tried:


    PHP Code:
      $ch curl_init();  
      
    $putData tmpfile();
      
    fwrite($putData$xml_request);
      
    fseek($putData0);
      
      
    $result_file fopen('/response_we_got.state.zip'"w");

      
    /* Set cURL options. */
      
    curl_setopt($chCURLOPT_URL'http://url_where_to_send.html');
      
    curl_setopt($chCURLOPT_PUTtrue);
      
    curl_setopt($chCURLOPT_INFILE$putData);
      
    curl_setopt($chCURLOPT_INFILESIZEstrlen($xml_request));
      
    curl_setopt($chCURLOPT_FILE$result_file);
      
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

      
    $result curl_exec($ch);  
      
    fclose($putData);        
      
    curl_close($ch); 
    This one I think would work just if I would be able to change the name of the INFILE element for "xmlfile"
    PHP Code:
    $postfields = array(
                        
    "xmlfile"     => "@/test_2_send.xml"
                        
    ); 
                        
    $pushUrl curl_init('http://url_where_to_send.html');
    curl_setopt($pushUrlCURLOPT_VERBOSE1);
    #curl_setopt($pushUrl, CURLOPT_FAILONERROR, 1);
    curl_setopt($pushUrlCURLOPT_FOLLOWLOCATION1);
    curl_setopt($pushUrlCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($pushUrlCURLOPT_POSTtrue);
    curl_setopt($pushUrlCURLOPT_FILE$result_file);
    curl_setopt($pushUrlCURLOPT_POSTFILEDS$postfields);
    $output curl_exec($pushUrl);

    curl_close($pushUrl); 

    From both of the solutions I am getting as response an error saying:

    Code:
    xmlfile is null, empty or doesn't exist.
    I would be really pleased if anyone would be able to help me.

    Thanks a lot,
    Álvaro
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Location
    Reno, NV
    Posts
    88
    Rep Power
    20
    After reading w ww.p hp.net/ma nual/en/fu nction.cu rl-set opt.php , I was able to write this:

    PHP Code:
    <?php
    $file_to_upload 
    = array('file_contents'=>'@'.$file_name_with_full_path);
    $ch curl_init();
    curl_setopt($chCURLOPT_URL,$target_url);
    curl_setopt($chCURLOPT_POST,1);
    curl_setopt($chCURLOPT_POSTFIELDS$file_to_upload);
    $result=curl_exec ($ch);
    curl_close ($ch);
    echo 
    $result;
    ?>
    which worked just like in example 2 on the PHP site.

    NOTE: On the URL above, I had to add spaces due to restrictions. I hope the admins don't get upset for me linking to the PHP site -- not like I'm trying to promote my own site.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2006
    Posts
    21
    Rep Power
    0
    Thank you very much for your answer. Surprisingly it has worked

    And I say that I am surprised because the only thing that differences your code, from mine is:

    YOURS
    Code:
    $file_to_upload = array('xmlfile'=>'@'.$file_name_with_full_path);
    MINE
    Code:
     $postfields = array("xmlfile"=> "@/full_path_2_send.xml");
    So I assume it is important to write the @ symbol outside the string of the file's path...

    Thanks again.

IMN logo majestic logo threadwatch logo seochat tools logo