aleostudio / salesforcerest
一个简单的PHP包,用于将SalesForce REST API集成到您的CRM中
1.0.0
2019-09-24 16:46 UTC
Requires
- php: >=7.2
- stevenmaguire/oauth2-salesforce: ^2.0
Requires (Dev)
- phpunit/phpunit: ^8.3@dev
This package is auto-updated.
Last update: 2024-09-25 04:57:16 UTC
README
一个简单的REST API集成,用于处理SalesForce CRM中的数据
安装
使用以下命令克隆包
git clone git@github.com:aleostudio/salesforcerest.git
使用以下命令安装其依赖项
composer install
如果您已经在项目中使用composer,并且想要使用此包,请使用以下命令要求它
composer require aleostudio/salesforcerest
SalesForce应用程序创建
- 访问 https://developer.salesforce.com/signup
- 注册用户并使用它进行身份验证。
- 从仪表板,在右上角菜单中,点击 切换到SalesForce Classic
- 现在,点击 设置(右上角),在快速查找框中输入 Apps,选择 Apps(在构建 | 创建下)
- 现在点击 连接应用 -> 新建(页面底部)。
- 输入所需的字段并点击 启用OAuth设置
- 指定您的 回调URL。它必须与您的应用程序的回调URL相同(只有https://有效)
- 选择 OAuth作用域:代表您在任何时候执行请求(refresh_token、offline_access)
- 点击 保存 时,将创建 Consumer Key 和 Consumer Secret。将它们复制到您的配置中
- 记住: clientId = Consumer Key,clientSecret = Consumer Secret,callbackUrl = Callback URL
代码示例
- 创建一个新的php文件并尝试以下代码(自定义自动加载路径)
<?php require_once __DIR__ . '/vendor/autoload.php'; use AleoStudio\SalesForceRest\SalesForceRest; // OAuth credentials. $config = [ 'clientId' => 'YOUR_CLIENT_ID', 'clientSecret' => 'YOUR_CLIENT_SECRET', 'callbackUrl' => 'https://your_domain/oauth_callback_url', ]; // Your stored data to avoid the authentication every time (empty object at the first time). $storedToken = (object) []; $storedToken = (object) [ 'accessToken' => 'YOUR_STORED_ACCESS_TOKEN', 'refreshToken' => 'YOUR_STORED_REFRESH_TOKEN', 'instanceUrl' => 'YOUR_STORED_INSTANCE_URL', 'tokenExpiry' => 'YOUR_STORED_TOKEN_EXPIRY_DATE' ]; // Main instance. $sf = new SalesForceRest($config); // If we already have a valid token object, we can bypass the auth flow. if (!empty((array) $storedToken)) { // Sets the stored token into our SalesForce instance. $sf->setToken($storedToken); } else { // OAuth authentication. // Set $authorize to true to force the auth or leave it to false if you want to use your stored token. $authorize = true; $sf->authentication($storedToken, $authorize); } // Query an entity using the SOQL syntax. $response = $sf->query('SELECT Id, Name, Title, FirstName, LastName, Email from Contact LIMIT 10'); foreach ($response['records'] as $row) { echo 'ID: '.$row['Id'].' - Name: '.$row['Name'].' - Email: '.$row['Email'].'<br/>'; } // Full entity fields list example. $fields = $sf->getEntityFields('Contact'); foreach ($fields as $field) { echo 'Name: '.$field['name'].' - Label: '.$field['label'].' - Type: '.$field['type'].'<br />'; } // CRUD methods list. $results = $sf->query('SELECT Id, Name from Contact LIMIT 100'); $new_id = $sf->create('Contact', ['FirstName'=>'John', 'LastName'=>'Doe', 'Email'=>'john.doe@domain.com']); $update = $sf->update('Contact', '0030b00002KgsnvAAB', ['FirstName'=>'Johnnnnn', 'LastName'=>'Doeeee', 'Title'=>null]); $delete = $sf->delete('Contact', '0030b00002KgsnvAAB'); // Helper methods list. $lists = $sf->getEntityLists('Contact'); $fields = $sf->getEntityFields('Contact'); $fOnList = $sf->getEntityFields('Contact', 'LIST_ID'); $item = $sf->getItem('Contact', '0030b00002KgsnvAAB'); $items = $sf->getItems('Contact', 'LIST_ID'); // Auth methods list. $authInstance = $sf->authentication((object)[], true); $tokenObject = $sf->getToken(); $accessToken = $sf->getAccessToken(); $instanceUrl = $sf->getInstanceUrl(); $sf->setToken($tokenObject);
单元测试
为您的操作系统安装PHPUnit
# On MacOS through Homebrew: brew install phpunit # On Linux Ubuntu/Debian: apt install phpunit # By sources: wget https://phar.phpunit.de/phpunit-8.3.4.phar chmod +x phpunit-8.3.4.phar sudo mv phpunit-8.3.4.phar /usr/local/bin/phpunit
使用以下命令运行测试
phpunit --bootstrap vendor/autoload.php tests/SalesForceRestTest.php
或通过Composer
composer test tests/SalesForceRestTest.php