Search code examples
phpxmlcoldfusionhttp-request

Translate PHP PUT HTTP request to 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.


Solution

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