Skip to content

Instantly share code, notes, and snippets.

@kevinlebrun
Created September 9, 2011 11:49
Show Gist options
  • Save kevinlebrun/f975583a41607ad24b61 to your computer and use it in GitHub Desktop.
Save kevinlebrun/f975583a41607ad24b61 to your computer and use it in GitHub Desktop.
CURL POST Json
#!/usr/bin/env php
<?php
$url = $argv[1];
$data = array('foo' => 'bar');
echo '>>>>> request : ' . $url . PHP_EOL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (false === $response) {
echo 'curl error : ' . curl_error($ch) . PHP_EOL;
} else {
echo 'response : ' . PHP_EOL;
var_dump(json_decode($response, true));
echo 'status : ' . $status . PHP_EOL;
}
echo '<<<<< end request' . PHP_EOL;
curl_close($ch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment