如何在不使用PHP将文件写入磁盘的情况下将文件发布到REST服务器?

时间:2009-06-08 21:01:59

标签: php rest curl

我正在尝试将XML文件作为内部API的POST方法的一部分发送到服务器。

所有PHP文档都指向使用$ postVars ['file'] ='@ / path / to / file.xml'来实际发送文件。

我想从字符串发送文件,但它仍然需要作为文件上传而不是字符串发送。

帮助?

2 个答案:

答案 0 :(得分:3)

看一下这个帖子,它会处理你想要做的事情我想:http://www.webmasterworld.com/php/3164561.htm

最后一个条目可能有帮助(由我重新格式化):

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; 
}

基本上,解决方案是使用内置的php流处理网址。

答案 1 :(得分:0)