如何在没有使用PHP将文件写入磁盘的情况下将文件POST到RES

如何在没有使用PHP将文件写入磁盘的情况下将文件POST到RES

本文介绍了如何在没有使用PHP将文件写入磁盘的情况下将文件POST到REST服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

帮助?

推荐答案

看看这个线程它处理你想做什么我认为:

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

基本上,解决方案是利用内置的php流处理urls。

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

这篇关于如何在没有使用PHP将文件写入磁盘的情况下将文件POST到REST服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:20