Skip to content

Instantly share code, notes, and snippets.

@ianwremmel
Created August 31, 2012 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianwremmel/3548403 to your computer and use it in GitHub Desktop.
Save ianwremmel/3548403 to your computer and use it in GitHub Desktop.
www.ianwremmel.com: PUTting PHP to Restful APIs (Like CouchDB) as JSON
<?php
foreach ($collection as $id => $data) {
$handle = fopen(FILENAME, 'w'€™);
$filesize = fwrite($handle, json_encode($data))
fclose($handle);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SERVER_URL . '/' . DATABASE . '/' . $id);
curl_setopt($ch, CURLOPT_PUT, TRUE);
$handle = fopen(FILENAME, 'r');
curl_setopt($ch, CURLOPT_INFILE, $handle);
curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
curl_exec($ch);
curl_close($ch);
fclose($handle);
}
?>
<?php
foreach ($collection as $id => $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SERVER_URL . '/' . DATABASE . '/' . $id);
curl_setopt($ch, CURLOPT_PUT, TRUE);
curl_setopt($ch, CURLOPT_INFILE, $data);
curl_exec($ch);
curl_close($ch);
}
?>
<?php
foreach ($collection as $id => $data) {
$handle = tmpfile
$filesize = fwrite($handle, json_encode($data)) - 2;
fseek($handle, 1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SERVER_URL . '/' . DATABASE . '/' . $id);
curl_setopt($ch, CURLOPT_PUT, TRUE);
curl_setopt($ch, CURLOPT_INFILE, $handle);
curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
curl_exec($ch);
curl_close($ch);
fclose($handle);
}
?>
<?php
foreach ($collection as $id => $data) {
$handle = tmpfile
$filesize = fwrite($handle, json_encode($data))
rewind($handle);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SERVER_URL . '/' . DATABASE . '/' . $id);
curl_setopt($ch, CURLOPT_PUT, TRUE);
curl_setopt($ch, CURLOPT_INFILE, $handle);
curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
curl_exec($ch);
curl_close($ch);
fclose($handle);
}
?>
<?php
foreach ($collection as $id => $data) {
$handle = tmpfile
$data = json_encode($data));
$length = strlen($data)
$data = substr(1, $data, $length) - 2;
$filesize = fwrite($handle, $data)
rewind($handle);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SERVER_URL . '/' . DATABASE . '/' . $id);
curl_setopt($ch, CURLOPT_PUT, TRUE);
curl_setopt($ch, CURLOPT_INFILE, $handle);
curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
curl_exec($ch);
curl_close($ch);
fclose($handle);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment