Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Created July 19, 2009 23:20
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 ericboehs/150074 to your computer and use it in GitHub Desktop.
Save ericboehs/150074 to your computer and use it in GitHub Desktop.
<?
#PHP REST client
$url = 'http://localhost:8080/classica/api/listings/5';
$method = 'PUT';
$headers = array(
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded'
);
$dataarr = array(
'title' => 'Pirate Ship',
'description' => 'Slightly battered pirate ship. 31ft long, great for pillaging!',
'price' => '1337.00',
'obo' => '0',
'listed' => '2009-07-01 12:34:56',
'flagged' => '0',
);
$data = '';
foreach($dataarr as $key => $value){
$data .= "$key=$value&";
}
$data = substr($data,0,-1);
$handle = curl_init();
$fdata = fopen('php://memory',"w+");
fwrite($fdata, $data);
rewind($fdata);
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
# sometimes we need this for https to work
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
switch($method)
{
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;
case 'PUT':
curl_setopt($handle, CURLOPT_PUT, true);
curl_setopt($handle, CURLOPT_INFILE, $fdata);
curl_setopt($handle, CURLOPT_INFILESIZE, strlen($data));
break;
case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
$raw_response = curl_exec($handle);
$response = json_decode($raw_response, true);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
echo "Status Code: $code<br />\n";
echo $raw_response;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment