5

这段代码在 ColdFusion 中会是什么样子?

  protected function httpPut($url, $params = null, $data = null)
  {
      $fh = fopen('php://memory', 'rw');
          fwrite($fh, $data);
          rewind($fh);

    $ch = curl_init($url);
    $this->addOAuthHeaders($ch, $url, $params['oauth']);
    curl_setopt($ch, CURLOPT_PUT, 1);
    curl_setopt($ch, CURLOPT_INFILE, $fh);
    curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $resp  = $this->curl->addCurl($ch);
    fclose($fh);
    return $resp;
  }

我有类似以下的内容,但它似乎不起作用。

<cffile action="write" file="d:\my\directory\path\test.xml" output="#arguments.requestXML#">
<cfhttp url="#oaAccessTokenURL#" method="#arguments.requestType#" charset="UTF-8">
    <cfheader name="Authorization" value="#oauthheader#">
    <cfhttpparam type="file" name="Course" file="d:\my\directory\path\test.xml">    
</cfhttp>

我对 PHP 的了解还不够,无法理解 $data 变量(只是一串 XML 数据)如何被放入 http 请求以及如何在 ColdFusion 中复制它。

4

3 回答 3

1

我会尝试将 method="put" 添加到您的 cfhttp 调用中。这将使 CFHTTP 发送正确的 http 动词(在这种情况下为 PUT)。

于 2010-08-10T22:46:12.640 回答
0

这是Java spark(来自Java docs),您需要解决它:

PutMethod put = new PutMethod("http://jakarta.apache.org");
        put.setRequestBody(new FileInputStream("UploadMe.gif"));

在 CF 中是这样翻译的:

<cfset myPut  = createObject("java", "org.apache.commons.httpclient.methods.PutMethod") />
<cfset myPut.init("http://example.com") />
<cfset myInputStream = createObject("java", "java.io.FileInputStream") />
<cfset myInputStream.init("myxml.xml") />
<cfset myPut.setRequestBody(myInputStream) />

等等...

在我粘贴在上面的链接中,您可以看到如下内容:

    URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(
    httpCon.getOutputStream());
out.write("Resource content");
out.close();

找到有效的 Java 解决方案并将其翻译成 CF。

编辑:

请参阅下面的评论以获取解决方案。

于 2010-08-09T20:58:51.980 回答
0

Assuming you are doing a PUT method, you can use ColdFusion's GetHttpRequestData() function to obtain the XHR data.

You can then save it out by doing something like this:

<cfset xhr_data = GetHttpRequestData() />
<cffile action="write" file="PATH/FILENAME" output="#xhr_data.content#">
于 2010-10-29T18:56:15.287 回答