2

I need to put a string of data like so: '< client>...<\client>' onto an XMl server (example url:'http://example.appspot.com/examples') using PHP. (Context: Adding a new client's details to the server).

I have tried using CURLOPT_PUT, with a file and with just a string (since it requires CURLOPT_INFILESIZE and CURLOPT_INFILE) but it does not work!

Are there any other PHP functions that could be used to do such a thing? I have been looking around but PUT requests information is sparse.

Thanks.

3 Answers 3

3
// Start curl  
    $ch = curl_init();  
// URL for curl  
    $url = "http://example.appspot.com/examples";  

// Put string into a temporary file  
    $putString = '<client>the RAW data string I want to send</client>';

/** use a max of 256KB of RAM before going to disk */  
    $putData = fopen('php://temp/maxmemory:256000', 'w');  
    if (!$putData) {  
        die('could not open temp memory data');  
    }  
fwrite($putData, $putString);  
fseek($putData, 0);  

// Headers  
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
// Binary transfer i.e. --data-BINARY  
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($ch, CURLOPT_URL, $url);  
// Using a PUT method i.e. -XPUT  
curl_setopt($ch, CURLOPT_PUT, true);  
// Instead of POST fields use these settings  
curl_setopt($ch, CURLOPT_INFILE, $putData);  
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putString));  

$output = curl_exec($ch);  
echo $output;  

// Close the file  
fclose($putData);  
// Stop curl  
curl_close($ch);  
Sign up to request clarification or add additional context in comments.

Comments

0

since I haven't worked with cURL so far I can't really answer to that topic. If you'd like to use cURL I'd suggest looking at the server log and see what actually didn't work (so: Was the output of the request really what it's supposed to be?)

If you don't mind switching over to another technology/library I'd suggest you to use the Zend HTTP Client which is really straight forward to use, simple to include and should satisfy all your needs. Especially as performing a PUT Request is as simple as that:

<?php 
   // of course, perform require('Zend/...') and 
   // $client = new Zend_HTTP_Client() stuff before
   // ...
   [...]
   $xml = '<yourxmlstuffhere>.....</...>';
   $client->setRawData($xml)->setEncType('text/xml')->request('PUT');
?>

Code sample is from: Zend Framework Docs # RAW-Data Requests

Comments

-1

Another way to add string body to the PUT request with CURL in PHP is:

 <?php
        $data = 'My string';
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // Define method type
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Set data to the body request
  ?>

I hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.