【问题标题】:How can I POST a file to a REST server without writing the file to disk with PHP?如何在不使用 PHP 将文件写入磁盘的情况下将文件发布到 REST 服务器?
【发布时间】:2010-11-01 07:22:24
【问题描述】:

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

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

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

帮助?

【问题讨论】:

    标签: php rest curl


    【解决方案1】:
    【解决方案2】:

    看看这个线程,它处理我认为你想做的事情: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 流处理 url。

    【讨论】:

    • 优秀。我会研究这个解决方案。
    猜你喜欢
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2021-04-23
    • 2017-10-22
    • 2022-06-29
    • 1970-01-01
    • 2011-06-14
    相关资源
    最近更新 更多