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 | |
---|
10 | include_once(INSTAGRAM_ROOT.'/Core/ApiException.php'); |
---|
11 | include_once(INSTAGRAM_ROOT.'/Net/ClientInterface.php'); |
---|
12 | |
---|
13 | |
---|
14 | /** |
---|
15 | * Curl Client |
---|
16 | * |
---|
17 | * Uses curl to access the API |
---|
18 | */ |
---|
19 | class Instagram_Net_CurlClient implements Instagram_Net_ClientInterface { |
---|
20 | |
---|
21 | /** |
---|
22 | * Curl Resource |
---|
23 | * |
---|
24 | * @var curl resource |
---|
25 | */ |
---|
26 | protected $curl = null; |
---|
27 | |
---|
28 | /** |
---|
29 | * Constructor |
---|
30 | * |
---|
31 | * Initializes the curl object |
---|
32 | */ |
---|
33 | function __construct(){ |
---|
34 | $this->initializeCurl(); |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * GET |
---|
39 | * |
---|
40 | * @param string $url URL to send get request to |
---|
41 | * @param array $data GET data |
---|
42 | * @return Instagram_Net_Response |
---|
43 | * @access public |
---|
44 | */ |
---|
45 | public function get( $url, array $data = null ){ |
---|
46 | curl_setopt( $this->curl, CURLOPT_CUSTOMREQUEST, 'GET' ); |
---|
47 | curl_setopt( $this->curl, CURLOPT_URL, sprintf( "%s?%s", $url, http_build_query( $data ) ) ); |
---|
48 | return $this->fetch(); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * POST |
---|
53 | * |
---|
54 | * @param string $url URL to send post request to |
---|
55 | * @param array $data POST data |
---|
56 | * @return Instagram_Net_Response |
---|
57 | * @access public |
---|
58 | */ |
---|
59 | public function post( $url, array $data = null ) { |
---|
60 | curl_setopt( $this->curl, CURLOPT_CUSTOMREQUEST, 'POST' ); |
---|
61 | curl_setopt( $this->curl, CURLOPT_URL, $url ); |
---|
62 | curl_setopt( $this->curl, CURLOPT_POSTFIELDS, http_build_query( $data ) ); |
---|
63 | return $this->fetch(); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * PUT |
---|
68 | * |
---|
69 | * @param string $url URL to send put request to |
---|
70 | * @param array $data PUT data |
---|
71 | * @return Instagram_Net_Response |
---|
72 | * @access public |
---|
73 | */ |
---|
74 | public function put( $url, array $data = null ){ |
---|
75 | curl_setopt( $this->curl, CURLOPT_CUSTOMREQUEST, 'PUT' ); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * DELETE |
---|
80 | * |
---|
81 | * @param string $url URL to send delete request to |
---|
82 | * @param array $data DELETE data |
---|
83 | * @return Instagram_Net_Response |
---|
84 | * @access public |
---|
85 | */ |
---|
86 | public function delete( $url, array $data = null ){ |
---|
87 | curl_setopt( $this->curl, CURLOPT_URL, sprintf( "%s?%s", $url, http_build_query( $data ) ) ); |
---|
88 | curl_setopt( $this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE' ); |
---|
89 | return $this->fetch(); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Initialize curl |
---|
94 | * |
---|
95 | * Sets initial parameters on the curl object |
---|
96 | * |
---|
97 | * @access protected |
---|
98 | */ |
---|
99 | protected function initializeCurl() { |
---|
100 | $this->curl = curl_init(); |
---|
101 | curl_setopt( $this->curl, CURLOPT_RETURNTRANSFER, true ); |
---|
102 | curl_setopt( $this->curl, CURLOPT_SSL_VERIFYPEER, false ); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * Fetch |
---|
107 | * |
---|
108 | * Execute the curl object |
---|
109 | * |
---|
110 | * @return StdClass |
---|
111 | * @access protected |
---|
112 | * @throws Instagram_Core_ApiException |
---|
113 | */ |
---|
114 | protected function fetch() { |
---|
115 | $raw_response = curl_exec( $this->curl ); |
---|
116 | $error = curl_error( $this->curl ); |
---|
117 | if ( $error ) { |
---|
118 | throw new Instagram_Core_ApiException( $error, 666, 'CurlError' ); |
---|
119 | } |
---|
120 | return $raw_response; |
---|
121 | } |
---|
122 | |
---|
123 | } |
---|
124 | ?> |
---|