Need help with a PHP Curl Post

Discussion in 'PHP' started by adzeds, Aug 2, 2011.

  1. #1
    I am working with an API that is asking me to do a http post to their server which has a tab-delimited batch file in the message-body.

    I am currently using curl to handle the HTTP Post but I am not sure how I would add a file to the message body?

    Anyone able to help?
     
    adzeds, Aug 2, 2011 IP
  2. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Like this:
    
    function execURL($url, $data = NULL){
            $postlen = count($data);
            foreach($data as $key=>$value) { $fields .= $key.'='.$value.'&'; }
            rtrim($fields,'&');
            $ch = curl_init();
            $timeout = 10;
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
            curl_setopt($ch,CURLOPT_POST,$postlen);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
            $response = curl_exec($ch);
            curl_close($ch);
            return $response;
        }
    
    PHP:
    $data being thearray of data you want to post.
    If you have 3 variables lets say - id, status and msg
    It should be -
    $data = array('id'=>$id, 'status'=>$status, 'msg'=>$msg);
     
    The Webby, Aug 3, 2011 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #3
    This usergroup email about posting files using curl might be helpful too

    http://groups.google.com/group/nzph...ba0/2a38883726052a79?lnk=gst#2a38883726052a79

     
    sarahk, Aug 7, 2011 IP