Skip to content

Instantly share code, notes, and snippets.

@virasak
Created April 9, 2013 15:58
Show Gist options
  • Save virasak/5346929 to your computer and use it in GitHub Desktop.
Save virasak/5346929 to your computer and use it in GitHub Desktop.
Simple JSON Client with cURL in PHP
<?php
class JSONClient {
private $opt_array = array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('Content-type: application/json'));
private $last_error = NULL;
public function __construct($opt = array()) {
if (count($opt) > 0) {
$opt_array = $opt;
}
}
public function post($url, $content) {
$handle = curl_init($url);
curl_setopt_array($handle, $this->opt_array);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($content));
$output_json = curl_exec($handle);
if ($output_json !== FALSE) {
$this->last_error = NULL;
return json_decode($output_json, TRUE);
} else {
$this->last_error = curl_error($handle);
return FALSE;
}
}
public function last_error() {
return $this->last_error;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment