将 PHP PUT HTTP 请求转换为 ColdFusion

发布于 2024-09-13 18:18:30 字数 1080 浏览 5 评论 0原文

这段代码在 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 中复制它。

What would this code look like in 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;
  }

I have something like the following, but it doesn't seem to be working.

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

I don't know enough about PHP to understand how the $data variable (which is just a string of XML data) is getting put into the http request and how to duplicate that in ColdFusion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽清梦 2024-09-20 18:18:30

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

I would try adding method="put" to your cfhttp call. That will make CFHTTP send the correct http verb (PUT in this case).

爱*していゐ 2024-09-20 18:18:30

这是 Java Spark(来自 Java 文档),您需要解决它:

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();

Findworking Java soution and translate it in CF 。

编辑:

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

Here's Java spark (from Java docs), you need to work it out:

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

is translated in CF like this:

<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) />

And so on...

In link I pasted above you can see somehting like this:

    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();

Find working Java soution and translate it in CF.

EDIT:

See comments below for a solution.

单身狗的梦 2024-09-20 18:18:30

假设您正在执行 PUT 方法,则可以使用 ColdFusion 的 GetHttpRequestData() 函数来获取 XHR 数据。

然后您可以通过执行以下操作将其保存:

<cfset xhr_data = GetHttpRequestData() />
<cffile action="write" file="PATH/FILENAME" output="#xhr_data.content#">

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#">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文