1

I am using PHP 5.3.6 and it seems I am unable to make a PUT request using CURL for PUTting just a string.

function put_data($url, $data)
{   
  $useragent="SimpleAgent-1.0";
  $fh = fopen('php://memory', 'rw');
  fwrite($fh, $data);
  rewind($fh);$ch = curl_init();
  curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  curl_setopt($ch, CURLOPT_INFILE, $fh);
  curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  curl_setopt($ch, CURLOPT_PUT, 1);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  curl_close($ch);
  fclose($fh);

  return $result;
}

Here, $data is the string that I want to PUT. Doesn't work and returns the following error:

500 Internal Server Error The server has either erred or is incapable of performing the requested operation.

expected string or buffer

Bijoy Thangaraj
  • 5,434
  • 4
  • 43
  • 70

2 Answers2

0

I am only able to pass the array as a data with the versions I am using. So this is what I am doing now:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_COOKIE, $curl_cookie);
$arr = array();
if (isset($data)) {
    $arr['my_data'] = $data;
}

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arr));
curl_exec($ch);
Bijoy Thangaraj
  • 5,434
  • 4
  • 43
  • 70
0

I used your code so defined a url and filled the data with a string and all worked as expected. Being I was rejected by the website as there was no receiving end that could deal with a put. To get info easily just add the line curl_setopt($ch, CURLOPT_VERBOSE, true);

and you will get something like:

* About to connect() to yyyy.xxxx.com port 80 (#0)
*   Trying 62.221.196.28...
* connected
* Connected to yyyy.xxxx.com (zz.221.196.28) port 80 (#0)
> PUT / HTTP/1.1
User-Agent: SimpleAgent-1.0
Host: yyyy.xxxx.com
Accept: */*
Content-Length: 14
Expect: 100-continue

< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 405 Method Not Allowed
< Date: Thu, 09 Feb 2012 19:46:28 GMT
< Server: Apache
< Allow: GET,HEAD,POST,OPTIONS
< Vary: Accept-Encoding
< Content-Length: 231
< Content-Type: text/html; charset=iso-8859-1
< 
* Connection #0 to host yyy.xxxx.com left intact
* Closing connection #0

As you can see from the logging the request went out, however when you want to put the data, the apache setting doesn't allow you to put data to that url. So depending upon the server you will have to take care of a receiving url that accepts the PUT.

Anton
  • 56
  • 5
  • 1
    Hi user1200241, Which version of PHP are you using? The code I posted seem to work in older versions of PHP. Do let me know. – Bijoy Thangaraj Feb 10 '12 at 04:34
  • Hi Bijoy I did upgrade to the latest php 5.3.10 version as I ran into memory leak problems when doing other tests. I ran your example under cli, again the most useful extension is the verbose option to get real progress information. Anton – Anton Feb 10 '12 at 10:32