Version 1
[yaffs-website] / vendor / php-instagram-api / php-instagram-api / Instagram / Net / CurlClient.php
1 <?php
2
3 /**
4 * Instagram PHP
5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
7 */
8
9 namespace Instagram\Net;
10
11 /**
12  * Curl Client
13  *
14  * Uses curl to access the API
15  */
16 class CurlClient implements ClientInterface {
17
18     /**
19      * Curl Resource
20      *
21      * @var curl resource
22      */
23     protected $curl = null;
24
25     /**
26      * Constructor
27      *
28      * Initializes the curl object
29      */
30     function __construct(){
31         $this->initializeCurl();
32     }
33
34     /**
35      * GET
36      *
37      * @param string $url URL to send get request to
38      * @param array $data GET data
39      * @return \Instagram\Net\Response
40      * @access public
41      */
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();
46     }
47
48     /**
49      * POST
50      *
51      * @param string $url URL to send post request to
52      * @param array $data POST data
53      * @return \Instagram\Net\Response
54      * @access public
55      */
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();
61     }
62
63     /**
64      * PUT
65      *
66      * @param string $url URL to send put request to
67      * @param array $data PUT data
68      * @return \Instagram\Net\Response
69      * @access public
70      */
71     public function put( $url, array $data = null  ){
72         curl_setopt( $this->curl, CURLOPT_CUSTOMREQUEST, 'PUT' );
73     }
74
75     /**
76      * DELETE
77      *
78      * @param string $url URL to send delete request to
79      * @param array $data DELETE data
80      * @return \Instagram\Net\Response
81      * @access public
82      */
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();
87     }
88
89     /**
90      * Initialize curl
91      *
92      * Sets initial parameters on the curl object
93      *
94      * @access protected
95      */
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 );
100     }
101
102     /**
103      * Fetch
104      *
105      * Execute the curl object
106      *
107      * @return StdClass
108      * @access protected
109      * @throws \Instagram\Core\ApiException
110      */
111     protected function fetch() {
112         $raw_response = curl_exec( $this->curl );
113         $error = curl_error( $this->curl );
114         if ( $error ) {
115             throw new \Instagram\Core\ApiException( $error, 666, 'CurlError' );
116         }
117         return $raw_response;
118     }
119     
120 }