tags:

views:

253

answers:

2

I'm trying to send an XML file to a server as part of the POST method for an internal API.

All the PHP documentation points to using the $postVars['file']='@/path/to/file.xml' to actually send the file.

I want to send the file from a string, but it still needs to be sent as a file upload, not a string.

Help?

+3  A: 

Take a look at this thread it deals with what you want to do I think: http://www.webmasterworld.com/php/3164561.htm

The last entry might be of help (reformatted by me):

function do_post_request($url, $data, $optional_headers = null) { 
  $params = array('http' => array( 
    'method' => 'post', 
    'content' => $data 
  )); 

  if ($optional_headers!== null) 
    $params['http']['header'] = $optional_headers; 

  $ctx = stream_context_create($params); 
  $fp = @fopen($url, 'rb', false, $ctx); 
  if (!$fp)
    throw new Exception("Problem with $url, $php_errormsg"); 

  $response = @stream_get_contents($fp); 
  if ($response === false) 
    throw new Exception("Problem reading data from $url, $php_errormsg"); 

  return $response; 
}

Basically the solution is to make use of the built-in php stream handling for urls.

Allain Lalonde
Excellent. I'll look into this solution.
Zachary Spencer
Works great! THanks!
Zachary Spencer