33namespace Webflow ;
44
55use Webflow \WebflowException ;
6- use GuzzleHttp \Client ;
76
87class Api {
98
109 const WEBFLOW_API_ENDPOINT = 'https://api.webflow.com ' ;
10+ const WEBFLOW_API_USERAGENT = 'ExpertLead Webflow PHP SDK (https://github.com/expertlead/webflow-php-sdk) ' ;
1111
1212 private $ client ;
13+ private $ token ;
1314
1415 function __construct (
1516 $ token ,
@@ -19,75 +20,114 @@ function __construct(
1920 throw new WebflowException ('token ' );
2021 }
2122
22- $ this ->client = new Client ([
23- 'base_uri ' => self ::WEBFLOW_API_ENDPOINT ,
24- 'headers ' => [
25- 'Authorization ' => "Bearer {$ token }" ,
26- 'accept-version ' => $ version ,
27- 'Accept ' => 'application/json ' ,
28- 'Content-Type ' => 'application/json ' ,
29- ]
30- ]);
23+ $ this ->token = $ token ;
24+ $ this ->version = $ version ;
3125
3226 return $ this ;
3327 }
3428
29+ private function request (string $ path , string $ method , array $ data = []) {
30+ $ curl = curl_init ();
31+ $ options = [
32+ CURLOPT_URL => self ::WEBFLOW_API_ENDPOINT . $ path ,
33+ CURLOPT_CUSTOMREQUEST => $ method ,
34+ CURLOPT_USERAGENT => self ::WEBFLOW_API_USERAGENT ,
35+ CURLOPT_HTTPHEADER => [
36+ "Authorization: Bearer {$ this ->token }" ,
37+ "accept-version: {$ this ->version }" ,
38+ "Accept: application/json " ,
39+ "Content-Type: application/json " ,
40+ ],
41+ CURLOPT_RETURNTRANSFER => true ,
42+ ];
43+ if (!empty ($ data )) {
44+ $ json = json_encode ($ data );
45+ $ options [CURLOPT_POSTFIELDS ] = $ json ;
46+ $ options [CURLOPT_HTTPHEADER ][] = "Content-Length: " . strlen ($ json );
47+ }
48+ curl_setopt_array ($ curl , $ options );
49+ $ response = curl_exec ($ curl );
50+ curl_close ($ curl );
51+ return $ this ->parse ($ response );
52+
53+ }
54+ private function get ($ path ) {
55+ return $ this ->request ($ path , "GET " );
56+ }
57+
58+ private function post ($ path , $ data ) {
59+ return $ this ->request ($ path , "POST " , $ data );
60+ }
61+
62+ private function put ($ path , $ data ) {
63+ return $ this ->request ($ path , "PUT " , $ data );
64+ }
65+
66+ private function delete ($ path , $ data ) {
67+ return $ this ->request ($ path , "DELETE " , $ data );
68+ }
69+
70+ private function parse ($ response ) {
71+ return json_decode ($ response );
72+ }
3573 // Meta
3674
3775 public function info () {
38- return $ this ->client -> get ('/info ' )-> getBody ( );
76+ return $ this ->get ('/info ' );
3977 }
4078
4179 public function sites () {
42- return $ this ->client -> get ('/sites ' )-> getBody ( );
80+ return $ this ->get ('/sites ' );
4381 }
4482
4583 public function site (string $ siteId ) {
46- return $ this ->client -> get ("/sites/ {$ siteId }" )-> getBody ( );
84+ return $ this ->get ("/sites/ {$ siteId }" );
4785 }
4886
4987 public function domains (string $ siteId ) {
50- return $ this ->client -> get ("/sites/ {$ siteId }/domains " )-> getBody ( );
88+ return $ this ->get ("/sites/ {$ siteId }/domains " );
5189 }
5290
5391 public function publishSite (string $ siteId , array $ domains ) {
54- return $ this ->client -> post (`/sites/ $ {siteId}/publish `, $ domains );
92+ return $ this ->post (`/sites/ $ {siteId}/publish `, $ domains );
5593 }
5694
5795 // Collections
5896
5997 public function collections (string $ siteId ) {
60- return $ this ->client -> get ("/sites/ {$ siteId }/collections " )-> getBody ( );
98+ return $ this ->get ("/sites/ {$ siteId }/collections " );
6199 }
62100
63101 public function collection (string $ collectionId ) {
64- return $ this ->client -> get ("/collections/ {$ collectionId }" )-> getBody ( );
102+ return $ this ->get ("/collections/ {$ collectionId }" );
65103 }
66104
67105 // Items
68106
69107 public function items (string $ collectionId ) {
70- return $ this ->client -> get ("/collections/ {$ collectionId }/items " )-> getBody ( );
108+ return $ this ->get ("/collections/ {$ collectionId }/items " );
71109 }
72110
73111 public function item (string $ collectionId , string $ itemId ) {
74- return $ this ->client -> get ("/collections/ {$ collectionId }/items/ {$ itemId }" )-> getBody ( );
112+ return $ this ->get ("/collections/ {$ collectionId }/items/ {$ itemId }" );
75113 }
76114
77- public function createItem (string $ collectionId , array $ data ) {
78- return $ this ->client ->post ("/collections/ {$ collectionId }/items " , [
79- 'json ' => [
80- 'fields ' => $ data ,
81- ],
82- ])->getBody ();
115+ public function createItem (string $ collectionId , array $ fields ) {
116+ $ defaults = [
117+ "_archived " => false ,
118+ "_draft " => false ,
119+ ];
120+ return $ this ->post ("/collections/ {$ collectionId }/items " , [
121+ 'fields ' => $ defaults + $ fields ,
122+ ]);
83123 }
84124
85- public function updateItem (string $ collectionId , string $ itemId , array $ data ) {
86- return $ this ->client -> put ("/collections/ {$ collectionId }/items/ {$ itemId }" , $ data );
125+ public function updateItem (string $ collectionId , string $ itemId , array $ fields ) {
126+ return $ this ->put ("/collections/ {$ collectionId }/items/ {$ itemId }" , $ fields );
87127 }
88128
89129 public function removeItem (string $ collectionId , $ itemId ) {
90- return $ this ->client -> delete ("/collections/ {$ collectionId }/items/ {$ itemId }" )-> getBody ( );
130+ return $ this ->delete ("/collections/ {$ collectionId }/items/ {$ itemId }" );
91131 }
92132
93133}
0 commit comments