首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mautic -我想通过api在mautic中添加联系人

mautic -我想通过api在mautic中添加联系人
EN

Stack Overflow用户
提问于 2018-06-10 15:07:29
回答 3查看 3.8K关注 0票数 2

我想通过API在mautic中添加联系人。下面我有代码,但它没有在mautic中添加联系人。

我已经在localhost中安装了mautic。研究了mautic文档中的API表单,并尝试了至少2天,但我没有得到任何结果。

代码语言:javascript
复制
<?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 = array(
                    'baseUrl'          => 'http://localhost/mautic',       // Base URL of the Mautic instance
                    'version'          => 'OAuth2', // Version of the OAuth can be OAuth2 or OAuth1a. OAuth2 is the default value.
                    'clientKey'        => '1_1w6nrty8k9og0kow48w8w4kww8wco0wcgswoow80ogkoo0gsks',       // Client/Consumer key from Mautic
                    'clientSecret'     => 'id6dow060fswcswgsgswgo4c88cw0kck4k4cc0wkg4gows08c',       // Client/Consumer secret key from Mautic
                    'callback'         => 'http://localhost/mtest/process.php'        // 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);
                /*
                if( $auth->getAccessTokenData() != null ) {
                        $accessTokenData = $auth->getAccessTokenData();
                        $settings['accessToken']        = $accessTokenData['access_token'];
                        $settings['accessTokenSecret']  = 'id6dow060fswcswgsgswgo4c88cw0kck4k4cc0wkg4gows08c'; //for OAuth1.0a
                        $settings['accessTokenExpires'] = $accessTokenData['expires']; //UNIX timestamp
                        $settings['refreshToken']       = $accessTokenData['refresh_token'];
                }*/
                // 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();
                            echo "<pre>";
                            print_r($accessTokenData);
                            echo "</pre>";
                            //store access token data however you want
                        }
                    }
                } catch (Exception $e) {
                    // Do Error handling
                }






                use Mautic\MauticApi;
                //use Mautic\Auth\ApiAuth;

                // ...
                $initAuth   = new ApiAuth();
                $auth       = $initAuth->newAuth($settings);
                $apiUrl     = "http://localhost/mautic/api";
                $api        = new MauticApi();
                $contactApi = $api->newApi("contacts", $auth, $apiUrl); 

                $data = array(
                    'firstname' => 'Jim',
                    'lastname'  => 'Contact',
                    'email'     => 'jim@his-site.com',
                    'ipAddress' => $_SERVER['REMOTE_ADDR']
                );

                $contact = $contactApi->create($data);
                echo "<br/>contact created";

任何帮助都将不胜感激。

EN

回答 3

Stack Overflow用户

发布于 2018-09-12 13:19:40

根据我的经验,有时在设置中激活API后,API只有在清除缓存后才开始工作。

  1. 确保您已在设置和清除缓存中激活了

cd /path/to/mautic

rm -rf app/cache/*

  • Then重试credentials)

  • 如果这不起作用,请尝试使用BasicAuth示例(您必须在设置中再次启用此选项,然后添加一个新用户来设置credentials)

我怀疑OAuth流可能受到本地设置/ SSL配置的干扰。

票数 3
EN

Stack Overflow用户

发布于 2020-03-22 17:22:37

代码语言:javascript
复制
   use Curl\Curl;
    
    $curl = new Curl();
    
    $un = 'mayank';
    $pw = 'mayank';
    $hash = base64_encode($un.':'.$pw);
    
    $curl->setHeader('Authorization','Basic '.$hash);
    $res = $curl->post(
        'http://mautic.local/api/contacts/new',
        [
            'firstname'=>'fn',
            'lastname'=>'ln',
            'email'=>'t1@test.com'
        ]
    );
    
    var_dump($res);

这是我尝试过的非常简单的东西,它对我很有效,请尝试清理缓存并启用日志记录,除非你提供一些错误,否则很难给你指明正确的方向。请检查app/logs目录和/var/ logs /apache2目录中的日志。

票数 2
EN

Stack Overflow用户

发布于 2020-05-13 04:51:30

这些步骤可能很有用:

  • 确保API已启用(是的,我知道这可能很明显,但仍然有效);
  • 检查日志;
  • 检查响应正文;
  • 尝试通过Postman

将其作为简单json发送<代码>F29

这可能是以下问题之一:

  • Cache;

  • 您没有发送必填自定义字段的键:

;;

  • 你把身份验证搞错了;

祝你好运:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50781355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档