PHP code example of garbetjie / wechat

1. Go to this page and download the library: Download garbetjie/wechat 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/ */

    

garbetjie / wechat example snippets


// Create a client instance.
$client = new \Garbetjie\WeChatClient\Client();
 
// Create a service instance.
$userService = new \Garbetjie\WeChatClient\Users\Service($client);

use Garbetjie\WeChatClient\Client;
use Garbetjie\WeChatClient\Authentication;
 
$appID = 'Your app ID';
$secret = 'Your secret key';
 
try {
    $authService = new Authentication\Service(new Client());
    $client = $authService->authenticate($appID, $secret);
} catch (Authentication\Exception $e) {
    // Handle errors.
}

$cacheDirectory = '/tmp';
$storage = new \Garbetjie\WeChatClient\Authentication\Storage\File($cacheDirectory);

$memcached = new \Memcached();
$memcached->addServer('127.0.0.1', 11211);
$keyPrefix = 'accessToken:';
 
$storage = new \Garbetjie\WeChatClient\Authentication\Storage\Memcached($memcached, $keyPrefix);

$pdo = new PDO('mysql:host=127.0.0.1;dbname=mydb', 'root', '');
$tableName = '_wechat_tokens';
$columnMapping = [
    'token'   => 'token_column_name',
    'hash'    => 'hash_column_name',
    'expires' => 'expiry_column_name',
];

$storage = new \Garbetjie\WeChatClient\Authentication\Storage\MySQL($pdo, $tableName, $columnMapping);

$groupService = new \Garbetjie\WeChatClient\Groups\Service($client);

$group = $groupService->createGroup("Test group");

$changedGroup = $group->withName('New test name');
$groupService->updateGroup($changedGroup);

$groupService->deleteGroup($group);

$groups = $groupService->getAllGroups();
 
foreach ($groups as $group) {
    echo sprintf(
        "Group #%d with name `%s` has %d user(s)\n",
        $group->getID(),
        $group->getName(),
        $group->getUserCount()
    );
}

$group = $groupService->getGroup(1);

$mediaService = new \Garbetjie\WeChatClient\Media\Service($client);

use Garbetjie\WeChatClient\Media\Type;
 
$imageMediaItem = new Type\Image('/path/to/image.jpg');
$uploadedMediaItem = $mediaService->upload($imageMediaItem);
 
// $uploadedMediaItem now has its ID and upload data populated:
$uploadedMediaItem->getMediaID();
$uploadedMediaItem->getExpiresDate();

$mediaService->download($uploadedMediaItem->getMediaID(), '/path/to/downloaded.jpg');

$fp = fopen('/tmp/downloaded.jpg', 'wb+');
$mediaService->download($uploadedMediaItem->getMediaID(), $fp);

$fp = $mediaService->download($uploadedMediaItem->getMediaID());
echo sprintf("Image is %d bytes in size", stream_get_length($fp));
fclose($fp);

$thumbnailMediaItem = new \Garbetjie\WeChatclient\Media\Type\Thumbnail('/path/to/thumbnail.jpg');

use Garbetjie\WeChatClient\Media\Type;
 
$newsItem = new Type\NewsItem('Article title', 'Content of the article.', $thumbnailMediaItem->getMediaID());
$newsItem = $newsItem->withAuthor('Author name');
$newsItem = $newsItem->withURL('http://example.org');
$newsItem = $newsItem->withSummary('Short summary blurb.');
$newsItem = $newsItem->withImageShowing(true);
 
$news = (new Type\News())->withItem($newsItem);

$audioMessage = new \Garbetjie\WeChatClient\Media\Type\Audio('/path/to/item.mp3');

$imageMessage = new \Garbetjie\WeChatClient\Media\Type\Image('/path/to/image.jpg');

$videoMediaItem = new \Garbetjie\WeChatClient\Media\Type\Video('/path/to/video.mp4');

$menuService = new \Garbetjie\WeChatClient\Menu\Service($client);

$qrService = new \Garbetjie\WeChatClient\QR\Service($client);

use Garbetjie\WeChatClient\QR;
 
$service = new QR\Service($client);
$temporaryCode1 = $service->createTemporaryCode(1000, 3600); // Expires in an hour.
$temporaryCode2 = $service->createTemporaryCode(1001); // Expires in 30 seconds.
$temporaryCode3 = $service->createTemporaryCode(1002, 2592000); // Expires in 30 days.

use Garbetjie\WeChatClient\QR;
 
$service = new QR\Service($client);
$permanentCode = $service->createPermanentCode(1000);
// OR
$permanentCode = $service->createPermanentCode('Look at me');