Created
April 9, 2013 15:58
-
-
Save virasak/5346929 to your computer and use it in GitHub Desktop.
Simple JSON Client with cURL in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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