عميل API المحدث قيد التطوير حاليًا في الفرع v2
عميل PHP للاتصال بـ Teamleader API.
الحزمة متاحة عبر الملحن:
$ composer require nascom/teamleader-api-client
أولاً، ستحتاج إلى توفير عميل يمكنه تقديم طلبات HTTP. يجب عليه تنفيذ HttpClientInterface
. العميل الذي يستخدم Guzzle متاح بالفعل في الحزمة. يتطلب هذا تثبيت guzzlehttp/guzzle.
<?php
use Nascom TeamleaderApiClient Http HttpClient GuzzleHttpClient ;
$ guzzle = new GuzzleHttp Client ([ ' base_uri ' => ' https://www.teamleader.be/api/ ' ]);
$ httpClient = new GuzzleHttpClient ( $ guzzle );
يمكنك استخدام HttpClient لإنشاء مثيل لعميل API الفعلي. سيتعين عليك تقديم بيانات اعتماد Teamleader API الخاصة بك أيضًا.
<?php
use Nascom TeamleaderApiClient Http ApiClient ApiClient ;
$ teamleaderParameters = [
' api_group ' => ' 12345 ' ,
' api_secret ' => ' XXXXXXXXXXXXXXX '
];
$ client = new ApiClient (
$ httpClient , // A client implementing HttpClientInterface.
$ teamleaderParameters // An array containing the Teamleader credentials.
);
تحتوي كل نقطة نهاية لواجهة برمجة التطبيقات (API) على فئة طلب مقابلة. يجب تمرير هذه الفئات إلى أسلوب handle()
الخاص بالعميل، والذي سيُرجع كائن استجابة. جميع الطلبات المتاحة يمكن العثور عليها هنا.
على سبيل المثال، إليك كيفية جلب تفاصيل المشروع:
<?php
use Nascom TeamleaderApiClient Request Project RetrieveProjectRequest ;
$ projectRequest = new RetrieveProjectRequest ( 23 );
$ response = $ client -> handle ( $ projectRequest );
echo $ response -> getData (); // Returns the Teamleader JSON response as a string.
يمكن تمرير الخيارات الإضافية إلى ApiClient. سيتم دمجها مع بعض الخيارات الافتراضية، وتمريرها إلى HttpClient عند تقديم الطلب.
<?php
$ client = new ApiClient (
$ httpClient ,
$ teamleaderParameters ,
[ ' connect_timeout ' => 5.0 ] // This will override the default timeout.
);
يمكنك إنشاء HttpClient مخصص للتعامل مع الطلبات. كل ما عليه فعله هو تنفيذ HttpClientInterface
. يمكنك، على سبيل المثال، تنفيذ عميل باستخدام الضفيرة.
<?php
use Nascom TeamleaderApiClient Http HttpClient HttpClientInterface ;
class CurlHttpClient implements HttpClientInterface
{
public function request ( $ method , $ uri , array $ options = [])
{
$ curl = curl_init ();
curl_setopt_array ( $ curl , array (
CURLOPT_RETURNTRANSFER => 1 ,
CURLOPT_URL => ' https://www.teamleader.be/api/ ' . $ uri ,
CURLOPT_POST => 1 ,
CURLOPT_POSTFIELDS => $ options [ ' form_params ' ]
));
$ response = curl_exec ( $ curl );
curl_close ( $ curl );
return $ response ;
}
}
$ client = new ApiClient (
new CurlHttpCLient (),
$ teamleaderParameters
);