Help us understand the problem. What is going on with this article?

PHP(Laravel)でSalesForceのREST APIを利用する

概要

LaravelでSalesForceのRESTAPIを利用する方法について調査したので記事として残しておく。
ここではよく使いそうなCRUD処理のサンプルを記載する。
※ Laravel特有の部分はあまりないので、他のフレームワークや生PHPでもいけます。

前提

CRUD処理サンプル

CREATE

    /**
     * SalesForce REST API Create
     * @Route /sample/sf/api/create
     * @Method POST
     */
    public function sfApiCreateRecord()
    {
        // 環境によって違うので自分の環境の値に変更してください
        $sfUrl = 'https://hoge.salesforce.com';
        $path  = '/services/data/v47.0/sobjects/account';

        $url   = $sfUrl . $path;
        $token = 'hogefugapiyo'; //tokenを自動で取得してくるような仕組みはこの記事では省略する

        $postData = [
            'Name' => 'nagi125',
            'Description' => 'REST API test',
        ];

        $params = [
            'headers' => [
                'Content-Type' => 'application/json',
                'Authorization' => 'Bearer '.$token,
            ],
            'body' => json_encode($postData),
        ];

        $client = new \GuzzleHttp\Client();
        $res = $client->request('POST', $url, $params);

        // 結果の確認
        dd($res->getBody()->getContents());
    }

READ

    /**
     * SalesForce REST API Read
     * @Route /sample/sf/api/read
     * @Method GET
     */
    public function sfApiReadRecord()
    {
        $sfUrl = 'https://hoge.salesforce.com';
        $path  = '/services/data/v47.0/sobjects/account/';
        $sfId  = 'hogefuga';

        // このようにSQLの結果も取得できる
        // $path = '/services/data/v47.0/query/?q=SELECT+Id,Name+From+Account';

        $url   = $sfUrl . $path . $sfId;
        $token = 'hogefugapiyo';

        $params = [
            'headers' => [
                'Authorization' => 'Bearer '.$token,
            ],
        ];

        $client = new \GuzzleHttp\Client();
        $res = $client->request('GET', $url, $params);

        // 結果の確認
        dd($res->getBody()->getContents());
    }

UPDATE

    /**
     * SalesForce REST API Update
     * @Route /sample/sf/api/update
     * @Method PATCH
     */
    public function sfApiUpdateRecord()
    {
        $sfUrl = 'https://hoge.salesforce.com';
        $path  = '/services/data/v47.0/sobjects/account/';
        $sfId  = 'hogefuga';

        $url   = $sfUrl . $path . $sfId;
        $token = 'hogefugapiyo';

        $postData = [
            'Name' => 'nagi125_update',
        ];

        $params = [
            'headers' => [
                'Content-Type' => 'application/json',
                'Authorization' => 'Bearer '.$token,
            ],
            'body' => json_encode($postData),
        ];

        $client = new \GuzzleHttp\Client();
        $res = $client->request('PATCH', $url, $params);

        // 結果の確認
        dd($res->getBody()->getContents());
    }

DELETE

    /**
     * SalesForce RestAPI Delete
     * @Route /sample/sf/api/delete
     * @Method DELETE
     */
    public function sfApiDeleteRecord()
    {
        $sfUrl = 'https://hoge.salesforce.com';
        $path  = '/services/data/v47.0/sobjects/account/';
        $sfId  = 'hogefuga';

        $url   = $sfUrl . $path . $sfId;
        $token = 'hogefugapiyo';

        $params = [
            'headers' => [
                'Authorization' => 'Bearer '.$token,
            ],
        ];

        $client = new \GuzzleHttp\Client();
        $res = $client->request('DELETE', $url, $params);

        // 結果の確認(deleteの場合、成功時に文字列が返ってこないのでStatusCodeで確認)
        dd($res->getStatusCode());
    }

その他

残りの操作について詳しく知りたい場合はREST APIの仕様書を見るとよいです。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした