1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: tareq
5+ * Date: 8/17/16
6+ * Time: 2:50 PM
7+ */
8+
9+ namespace PHPShopify ;
10+
11+
12+ use PHPShopify \Exception \CurlException ;
13+
14+ class CurlRequest
15+ {
16+ public static function init ($ url , $ httpHeaders = array ())
17+ {
18+ // Create Curl resource
19+ $ ch = curl_init ();
20+
21+ // Set URL
22+ curl_setopt ($ ch , CURLOPT_URL , $ url );
23+
24+ //Return the transfer as a string
25+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
26+
27+ $ headers = array ();
28+ foreach ($ httpHeaders as $ key => $ value ) {
29+ $ headers [] = "$ key: $ value " ;
30+ }
31+ //Set HTTP Headers
32+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
33+
34+ return $ ch ;
35+
36+ }
37+
38+ public static function get ($ url , $ httpHeaders = array ())
39+ {
40+ //Initialize the Curl resource
41+ $ ch = self ::init ($ url , $ httpHeaders );
42+
43+ return self ::processRequest ($ ch );
44+ }
45+
46+ public static function post ($ url , $ data , $ httpHeaders = array ())
47+ {
48+ $ ch = self ::init ($ url , $ httpHeaders );
49+ //Set the request type
50+ curl_setopt ($ ch , CURLOPT_CUSTOMREQUEST , 'POST ' );
51+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ data );
52+
53+ return self ::processRequest ($ ch );
54+ }
55+
56+ public static function put ($ url , $ data , $ httpHeaders = array ())
57+ {
58+ $ ch = self ::init ($ url , $ httpHeaders );
59+ //set the request type
60+ curl_setopt ($ ch , CURLOPT_CUSTOMREQUEST , 'PUT ' );
61+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ data );
62+
63+ return self ::processRequest ($ ch );
64+ }
65+
66+ public static function delete ($ url , $ httpHeaders = array ())
67+ {
68+ $ ch = self ::init ($ url , $ httpHeaders );
69+ //set the request type
70+ curl_setopt ($ ch , CURLOPT_CUSTOMREQUEST , 'DELETE ' );
71+
72+ return self ::processRequest ($ ch );
73+ }
74+
75+ public static function processRequest ($ ch )
76+ {
77+ // $output contains the output string
78+ $ output = curl_exec ($ ch );
79+
80+ if (curl_errno ($ ch )) {
81+ throw new Exception \CurlException (curl_errno ($ ch ) . ' : ' . curl_error ($ ch ));
82+ }
83+
84+ // close curl resource to free up system resources
85+ curl_close ($ ch );
86+
87+ return $ output ;
88+ }
89+ }
0 commit comments