คุณสามารถติดตั้งไลบรารี API ด้วยคำสั่งต่อไปนี้:
composer require mautic/api-library NB ตรวจสอบให้แน่ใจว่าคุณได้ติดตั้งไคลเอนต์ PSR-18 HTTP ก่อนที่คุณจะติดตั้งแพ็คเกจนี้หรือติดตั้งหนึ่งในเวลาเดียวกันเช่น composer require mautic/api-library guzzlehttp/guzzle:^7.3
เราถูกแยกออกจากไคลเอนต์การส่งข้อความ HTTP ใด ๆ ด้วยความช่วยเหลือของไคลเอนต์ PSR-18 HTTP สิ่งนี้ต้องใช้แพ็คเกจพิเศษที่ให้บริการ PSR/HTTP-CLIENTEMPLEMENTATION ในการใช้ Guzzle 7 เพียงแค่ต้องการ guzzlehttp/guzzle :
composer require guzzlehttp/guzzle:^7.3ไคลเอนต์ HTTP ที่ติดตั้งนั้นถูกค้นพบโดยอัตโนมัติโดยใช้ PHP-HTTP/Discovery แต่คุณยังสามารถให้ไคลเอนต์ HTTP ของคุณเองได้หากต้องการ
<?php
// Bootup the Composer autoloader
include __DIR__ . ' /vendor/autoload.php ' ;
use GuzzleHttp Client ;
use Mautic Auth ApiAuth ;
// Initiate an HTTP Client
$ httpClient = new Client ([
' timeout ' => 10 ,
]);
// Initiate the auth object
$ initAuth = new ApiAuth ( $ httpClient );
$ auth = $ initAuth -> newAuth ( $ settings );
// etc. API จะต้องเปิดใช้งานใน Mautic ภายใน Mautic ให้ไปที่หน้าการกำหนดค่า (อยู่ในเมนูการตั้งค่า) และภายใต้การตั้งค่า API เปิดใช้งาน API ของ Mautic หากคุณตั้งใจจะใช้การรับรองความถูกต้องขั้นพื้นฐานให้แน่ใจว่าคุณเปิดใช้งาน คุณยังสามารถเลือกโปรโตคอล OAuth ที่จะใช้ที่นี่ หลังจากบันทึกการกำหนดค่าให้ไปที่หน้ารับรอง API (อยู่ในเมนูการตั้งค่า) และสร้างไคลเอนต์ใหม่ ป้อนการโทรกลับ/เปลี่ยนเส้นทาง URI ว่าคำขอจะถูกส่งจาก คลิกสมัครจากนั้นคัดลอกรหัสไคลเอนต์และความลับไคลเอนต์ไปยังแอปพลิเคชันที่จะใช้ API
ขั้นตอนแรกคือการได้รับอนุญาต Mautic รองรับ OAuth 1.0a และ Oauth 2 อย่างไรก็ตามมันขึ้นอยู่กับผู้ดูแลระบบที่จะตัดสินใจว่าจะเปิดใช้งาน ดังนั้นจึงเป็นการดีที่สุดที่จะมีตัวเลือกการกำหนดค่าภายในโครงการของคุณเพื่อให้ผู้ดูแลระบบเลือกรหัสของคุณควรใช้วิธีใด
<?php
// Bootup the Composer autoloader
include __DIR__ . ' /vendor/autoload.php ' ;
use Mautic Auth ApiAuth ;
session_start ();
$ publicKey = '' ;
$ secretKey = '' ;
$ callback = '' ;
// ApiAuth->newAuth() will accept an array of Auth settings
$ settings = [
' baseUrl ' => '' , // Base URL of the Mautic instance
' version ' => ' OAuth2 ' , // Version of the OAuth can be OAuth2 or OAuth1a. OAuth2 is the default value.
' clientKey ' => '' , // Client/Consumer key from Mautic
' clientSecret ' => '' , // Client/Consumer secret key from Mautic
' callback ' => '' , // Redirect URI/Callback URI for this script
];
/*
// If you already have the access token, et al, pass them in as well to prevent the need for reauthorization
$settings['accessToken'] = $accessToken;
$settings['accessTokenSecret'] = $accessTokenSecret; //for OAuth1.0a
$settings['accessTokenExpires'] = $accessTokenExpires; //UNIX timestamp
$settings['refreshToken'] = $refreshToken;
*/
// Initiate the auth object
$ initAuth = new ApiAuth ();
$ auth = $ initAuth -> newAuth ( $ settings );
// Initiate process for obtaining an access token; this will redirect the user to the $authorizationUrl and/or
// set the access_tokens when the user is redirected back after granting authorization
// If the access token is expired, and a refresh token is set above, then a new access token will be requested
try {
if ( $ auth -> validateAccessToken ()) {
// Obtain the access token returned; call accessTokenUpdated() to catch if the token was updated via a
// refresh token
// $accessTokenData will have the following keys:
// For OAuth1.0a: access_token, access_token_secret, expires
// For OAuth2: access_token, expires, token_type, refresh_token
if ( $ auth -> accessTokenUpdated ()) {
$ accessTokenData = $ auth -> getAccessTokenData ();
//store access token data however you want
}
}
} catch ( Exception $ e ) {
// Do Error handling
}แทนที่จะยุ่งกับ OAuth คุณอาจเลือกใช้ BasicAuth แทน
นี่คือเวอร์ชันพื้นฐานของรหัสด้านบน
<?php
// Bootup the Composer autoloader
include __DIR__ . ' /vendor/autoload.php ' ;
use Mautic Auth ApiAuth ;
session_start ();
// ApiAuth->newAuth() will accept an array of Auth settings
$ settings = [
' userName ' => '' , // Create a new user
' password ' => '' , // Make it a secure password
];
// Initiate the auth object specifying to use BasicAuth
$ initAuth = new ApiAuth ();
$ auth = $ initAuth -> newAuth ( $ settings , ' BasicAuth ' );
// Nothing else to do ... It's ready to use.
// Just pass the auth object to the API context you are creating.หมายเหตุ: หากข้อมูลรับรองไม่ถูกต้องการตอบกลับข้อผิดพลาดจะถูกส่งคืน
[
' errors ' => [
[
' code ' => 403 ,
' message ' => ' access_denied: OAuth2 authentication required ' ,
' type ' => ' access_denied ' ,
],
],
];ตอนนี้คุณมีโทเค็นการเข้าถึงและวัตถุ Auth คุณสามารถทำคำขอ API ได้ API แบ่งออกเป็นบริบท
<?php
use Mautic MauticApi ;
// Create an api context by passing in the desired context (Contacts, Forms, Pages, etc), the $auth object from above
// and the base URL to the Mautic server (i.e. http://my-mautic-server.com/api/)
$ api = new MauticApi ();
$ contactApi = $ api -> newApi ( ' contacts ' , $ auth , $ apiUrl );บริบทที่ได้รับการสนับสนุนอยู่ในปัจจุบัน:
ดูเอกสารนักพัฒนา
บริบททั้งหมดข้างต้นสนับสนุนฟังก์ชั่นต่อไปนี้สำหรับการดึงรายการ:
<?php
$ response = $ contactApi -> get ( $ id );
$ contact = $ response [ $ contactApi -> itemName ()];
// getList accepts optional parameters for filtering, limiting, and ordering
$ response = $ contactApi -> getList ( $ filter , $ start , $ limit , $ orderBy , $ orderByDir );
$ totalContacts = $ response [ ' total ' ];
$ contact = $ response [ $ contactApi -> listName ()]; <?php
$ fields = $ contactApi -> getFieldList ();
$ data = array ();
foreach ( $ fields as $ field ) {
$ data [ $ field [ ' alias ' ]] = $ _POST [ $ field [ ' alias ' ]];
}
// Set the IP address the contact originated from if it is different than that of the server making the request
$ data [ ' ipAddress ' ] = $ ipAddress ;
// Create the contact
$ response = $ contactApi -> create ( $ data );
$ contact = $ response [ $ contactApi -> itemName ()]; <?php
$ updatedData = [
' firstname ' => ' Updated Name '
];
$ response = $ contactApi -> edit ( $ contactId , $ updatedData );
$ contact = $ response [ $ contactApi -> itemName ()];
// If you want to create a new contact in the case that $contactId no longer exists
// $response will be populated with the new contact item
$ response = $ contactApi -> edit ( $ contactId , $ updatedData , true );
$ contact = $ response [ $ contactApi -> itemName ()]; <?php
$ response = $ contactApi -> delete ( $ contactId );
$ contact = $ response [ $ contactApi -> itemName ()]; <?php
// $response returned by an API call should be checked for errors
$ response = $ contactApi -> delete ( $ contactId );
if ( isset ( $ response [ ' errors ' ])) {
foreach ( $ response [ ' errors ' ] as $ error ) {
echo $ error [ ' code ' ] . " : " . $ error [ ' message ' ];
}
}เพื่อเริ่มต้นอย่างรวดเร็วเราขอแนะนำให้คุณใช้ DDEV ซึ่งตั้งค่าสิ่งต่าง ๆ ให้คุณโดยอัตโนมัติ มันโคลนนิ่ง https://github.com/mautic/mautic ตั้งค่าอินสแตนซ์ท้องถิ่นสำหรับคุณและเชื่อมต่อการทดสอบไลบรารี API กับอินสแตนซ์นั้น
ในการเริ่มต้นใช้งาน ddev start ! ประสบการณ์ครั้งแรกของเราจะแนะนำคุณเกี่ยวกับการตั้งค่า
หากคุณต้องการตั้งค่าสภาพแวดล้อมในพื้นที่ของคุณด้วยตนเองตรวจสอบให้แน่ใจว่าคุณคัดลอก /tests/local.config.php.dist ถึง /tests/local.config.php และกรอกการตั้งค่าที่จำเป็น เราขอแนะนำให้ใช้วิธีการตรวจสอบความถูกต้องพื้นฐานเพื่อให้ได้อย่างรวดเร็ว
กำหนดค่าการทดสอบหน่วยการทดสอบก่อนที่จะเรียกใช้การทดสอบหน่วย การทดสอบ Fire API Real Real Real ไปยังอินสแตนซ์ Mautic
composer test เพื่อเรียกใช้การทดสอบ แก้ไขคำสั่งนี้เพื่อเรียกใช้การทดสอบเฉพาะ: composer test -- --filter testCreateGetAndDelete tests/Api/NotesTest.php
แก้ไขคำสั่งนี้เพื่อเรียกใช้การทดสอบทั้งหมดในคลาสเดียว: composer test -- --filter test tests/Api/NotesTest.php
ขอบคุณไปที่คนที่ยอดเยี่ยมเหล่านี้ (คีย์อีโมจิ):
Zdeno Kuzmany | Dlopez-Akalam | มอลลักซ์ | Martina Scholz | John Linhart - | Marinus van Velzen | ปิแอร์อัมเมลูต - |
Martin Vooremäe |
โครงการนี้เป็นไปตามข้อกำหนดทั้งหมดของผู้เข้าร่วม การมีส่วนร่วมทุกชนิดยินดีต้อนรับ!