5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
9 namespace Instagram\Net;
14 * Uses curl to access the API
16 class CurlClient implements ClientInterface {
23 protected $curl = null;
28 * Initializes the curl object
30 function __construct(){
31 $this->initializeCurl();
37 * @param string $url URL to send get request to
38 * @param array $data GET data
39 * @return \Instagram\Net\Response
42 public function get( $url, array $data = null ){
43 curl_setopt( $this->curl, CURLOPT_CUSTOMREQUEST, 'GET' );
44 curl_setopt( $this->curl, CURLOPT_URL, sprintf( "%s?%s", $url, http_build_query( $data ) ) );
45 return $this->fetch();
51 * @param string $url URL to send post request to
52 * @param array $data POST data
53 * @return \Instagram\Net\Response
56 public function post( $url, array $data = null ) {
57 curl_setopt( $this->curl, CURLOPT_CUSTOMREQUEST, 'POST' );
58 curl_setopt( $this->curl, CURLOPT_URL, $url );
59 curl_setopt( $this->curl, CURLOPT_POSTFIELDS, http_build_query( $data ) );
60 return $this->fetch();
66 * @param string $url URL to send put request to
67 * @param array $data PUT data
68 * @return \Instagram\Net\Response
71 public function put( $url, array $data = null ){
72 curl_setopt( $this->curl, CURLOPT_CUSTOMREQUEST, 'PUT' );
78 * @param string $url URL to send delete request to
79 * @param array $data DELETE data
80 * @return \Instagram\Net\Response
83 public function delete( $url, array $data = null ){
84 curl_setopt( $this->curl, CURLOPT_URL, sprintf( "%s?%s", $url, http_build_query( $data ) ) );
85 curl_setopt( $this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE' );
86 return $this->fetch();
92 * Sets initial parameters on the curl object
96 protected function initializeCurl() {
97 $this->curl = curl_init();
98 curl_setopt( $this->curl, CURLOPT_RETURNTRANSFER, true );
99 curl_setopt( $this->curl, CURLOPT_SSL_VERIFYPEER, false );
105 * Execute the curl object
109 * @throws \Instagram\Core\ApiException
111 protected function fetch() {
112 $raw_response = curl_exec( $this->curl );
113 $error = curl_error( $this->curl );
115 throw new \Instagram\Core\ApiException( $error, 666, 'CurlError' );
117 return $raw_response;