1. Go to this page and download the library: Download helpscout/api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
helpscout / api example snippets
use HelpScout\Api\ApiClientFactory;
$client = ApiClientFactory::createClient();
// Set Client credentials if using that grant type. Using this approach will lazily fetch an access token on-demand
// when needed.
$client->useClientCredentials($appId, $appSecret);
// Set Access token directly if you have it
$client->setAccessToken('abc123');
// Use a refresh token to get a new access token
$client->useRefreshToken($appId, $appSecret, $refreshToken);
$client = ApiClientFactory::createClient([], function (Authenticator $authenticator) {
// This $authenticator contains the refreshed token
echo 'New token: '.$authenticator->accessToken().PHP_EOL;
});
use HelpScout\Api\Http\Auth\HandlesTokenRefreshes;
use HelpScout\Api\Http\Authenticator;
$callback = new class implements HandlesTokenRefreshes {
public function whenTokenRefreshed(Authenticator $authenticator)
{
// @todo Persist the token
}
};
$client = ApiClientFactory::createClient([], $callback);
use HelpScout\Api\Entity\Collection;
use HelpScout\Api\Entity\Patch;
$operations = new Collection(
[
new Patch('remove', 'property-1'),
new Patch('replace', 'property-2', 'value'),
];
);
$client->customerEntry()->updateProperties($customerId, $operations);
$mailbox = $client->mailboxes()->get($mailboxId);
use HelpScout\Api\Mailboxes\MailboxRequest;
$request = (new MailboxRequest)
->withFields()
->withFolders();
$mailbox = $client->mailboxes()->get($mailboxId, $request);
$fields = $mailbox->getFields();
$folders = $mailbox->getFolders();
$mailboxes = $client->mailboxes()->list();
use HelpScout\Api\Mailboxes\MailboxRequest;
$request = (new MailboxRequest)
->withFields()
->withFolders();
$mailboxes = $client->mailboxes()->list($request);
use HelpScout\Api\Conversations\ConversationRequest;
use HelpScout\Api\Conversations\ConversationFilters;
$request = (new ConversationRequest)
->withMailbox()
->withThreads();
$filters = (new ConversationFilters())
->inMailbox(1)
->inFolder(13)
->byCustomField(123, 'blue');
$conversations = $client->conversations()->list($filters, $request);
$customField = new CustomField();
$customField->setId(10524);
$customField->setValue(new DateTime('today'));
$client->conversations()->updateCustomFields($conversationId, [$customField]);
// We can specify either the id or email for the Customer
$customer = new Customer();
$customer->addEmail('[email protected]');
$thread = new CustomerThread();
$thread->setCustomer($customer);
$thread->setText('Test');
$conversation = new Conversation();
$conversation->setSubject('Testing the PHP SDK v2: Phone Thread');
$conversation->setStatus('active');
$conversation->setType('email');
$conversation->setMailboxId(80261);
$conversation->setCustomer($customer);
$conversation->setThreads(new Collection([
$thread,
]));
// You can optionally add tags
$tag = new Tag();
$tag->setName('testing');
$conversation->addTag($tag);
try {
$conversationId = $client->conversations()->create($conversation);
} catch (ValidationErrorException $e) {
var_dump($e->getError()->getErrors());
}
$client->conversations()->move($conversationId, 18);
$client->conversations()->updateSubject($conversationId, 'Need more help please');
$client->conversations()->updateCustomer($conversationId, 6854);
$client->conversations()->publishDraft($conversationId);
$client->conversations()->updateStatus($conversationId, 'closed');
$client->conversations()->assign($conversationId, 127);
$client->conversations()->unassign($conversationId);
use HelpScout\Api\Customers\Customer;
use HelpScout\Api\Conversations\Threads\ChatThread;
$thread = new ChatThread();
$customer = new Customer();
$customer->setId(163487350);
$thread->setCustomer($customer);
$thread->setText('Thanks for reaching out to us!');
$client->threads()->create($conversationId, $thread);
use HelpScout\Api\Customers\Customer;
use HelpScout\Api\Conversations\Threads\CustomerThread;
$thread = new CustomerThread();
$customer = new Customer();
$customer->setId(163487350);
$thread->setCustomer($customer);
$thread->setText('Please help me figure this out');
$client->threads()->create($conversationId, $thread);
use HelpScout\Api\Conversations\Threads\NoteThread;
$thread->setText('We are still looking into this');
$client->threads()->create($conversationId, $thread);
use HelpScout\Api\Customers\Customer;
use HelpScout\Api\Conversations\Threads\PhoneThread;
$thread = new PhoneThread();
$customer = new Customer();
$customer->setId(163487350);
$thread->setCustomer($customer);
$thread->setText('This customer called and spoke with us directly about the delay on their order');
$client->threads()->create($conversationId, $thread);
use HelpScout\Api\Customers\Customer;
use HelpScout\Api\Conversations\Threads\ReplyThread;
$thread = new ReplyThread();
$customer = new Customer();
$customer->setId(163487350);
$thread->setCustomer($customer);
$thread->setText("Thanks, we'll be with you shortly!");
$client->threads()->create($conversationId, $thread);
use HelpScout\Api\Users\UserFilters;
$filters = (new UserFilters())
->inMailbox(1)
->byEmail('[email protected]');
$users = $client->users()->list($filters);
// Example of running the Company Overall Report
// https://developer.helpscout.com/mailbox-api/endpoints/reports/company/reports-company-overall/
use HelpScout\Api\Reports\Company;
$params = [
// Date interval fields can be passed as an object implementing the \DateTimeInterface
// or as a string in the 'Y-m-d\Th:m:s\Z' format. All times should be in UTC.
'start' => new \DateTime('-7 days'),
'end' => new \DateTimeImmutable(),
'previousStart' => '2015-01-01T00:00:00Z',
'previousEnd' => '2015-01-31T23:59:59Z',
// Fields accepting multiple values can be passed as an array or a comma-separated string
'mailboxes' => [123, 321],
'tags' => '987,789',
'types' => ['chat', 'email'],
'folders' => [111, 222]
];
$report = $client->runReport(Company\Overall::class, $params);
// Build it from globals
$incoming = IncomingWebhook::makeFromGlobals($secret);
// or build using a request object that satisfies the PSR-7 RequestInterface
/** @var RequestInterface $request */
$request = new Request(...);
$secret = 'superSekretKey';
$incoming = new IncomingWebhook($request, $secret);
try {
// do something
} catch (\HelpScout\Api\Exception\ValidationErrorException $e) {
$error = $e->getError();
var_dump(
// A reference id for that request. Including this anytime you contact Help Scout will
// empower us to dig right to the heart of the issue
$error->getCorrelationId(),
// Details about the invalid fields in the request
$error->getErrors()
);
exit;
}
/** @var PagedCollection $users */
$users = $client->users()->list();
// Iterate over the first page of results
foreach ($users as $user) {
echo $users->getFirstName();
}
// The current page number
$users->getPageNumber();
// The total number of pages
$users->getTotalPageCount();
// Load the next page
$nextUsers = $users->getNextPage();
// Load the previous page
$previousUsers = $users->getPreviousPage();
// Load the first page
$firstUsers = $users->getFirstPage();
// Load the last page
$lastUsers = $users->getLastPage();
// Load a specific page
$otherUsers = $users->getPage(12);
// Paged results are accessible as normal arrays, so you can simply iterate over them
foreach ($otherUsers as $user) {
echo $user->getFirstName();
}
// From within the tests/ApiClientTest.php file...
public function testMockReturnsProperMock()
{
$client = ApiClientFactory::createClient();
$mockedWorkflows = $client->mock('workflows');
$this->assertInstanceOf(WorkflowsEndpoint::class, $mockedWorkflows);
$this->assertInstanceOf(MockInterface::class, $mockedWorkflows);
$this->assertSame(
$mockedWorkflows,
$client->workflows()
);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.