【问题标题】:Using PHP to insert records to Zoho CRM via the API使用 PHP 通过 API 将记录插入 Zoho CRM
【发布时间】:2012-04-15 02:39:58
【问题描述】:

我认为使用 get_file_contents 函数可以让我像使用过去使用的其他 API 一样执行 API。然而,这种方法不适用于 Zoho CRM API - 可能是因为我传递的是 XML 数据而不是 RESTful 查询?

API 文档位于 http://zohocrmapi.wiki.zoho.com/insertRecords-Method.html

当通过网络浏览器地址栏传递它时,它可以工作:

https://crm.zoho.com/crm/private/xml/Contacts/insertRecords?authtoken=Auth Token&scope=crmapi
&newFormat=1
&xmlData=
<Contacts>
<row no="1">
<FL val="First Name">Scott</FL>
<FL val="Last Name">James</FL>
<FL val="Email">test@test.com</FL>
<FL val="Department">CG</FL>
<FL val="Phone">999999999</FL>
<FL val="Fax">99999999</FL>
<FL val="Mobile">99989989</FL>
<FL val="Assistant">John</FL>
</row>
</Contacts>

使用 file_get_contents 运行此程序时,我没有收到任何错误。有谁知道我需要做什么才能让它工作?

【问题讨论】:

    标签: api crm zoho


    【解决方案1】:
       <?php
    $xml = 
            '<?xml version="1.0" encoding="UTF-8"?>
            <Contacts>
            <row no="1">
            <FL val="First Name">Digant</FL>
            <FL val="Last Name">Shah1</FL>
            <FL val="Email">digant.shah91@gmail.com</FL>
            <FL val="Department">php</FL>
            <FL val="Phone">999999999</FL>
            <FL val="Fax">99999999</FL>
            <FL val="Mobile">99989989</FL>
            <FL val="Assistant">none</FL>
            </row>
            </Contacts>';
    $auth="*******************";
        $url ="https://crm.zoho.com/crm/private/xml/Contacts/insertRecords";
        $query="authtoken=".$auth."&scope=crmapi&newFormat=1&xmlData=".$xml;
        $ch = curl_init();
        /* set url to send post request */
        curl_setopt($ch, CURLOPT_URL, $url);
        /* allow redirects */
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        /* return a response into a variable */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        /* times out after 30s */
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        /* set POST method */
        curl_setopt($ch, CURLOPT_POST, 1);
        /* add POST fields parameters */
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query);// Set the request as a POST FIELD for curl.
    
        //Execute cUrl session
        $response = curl_exec($ch);
        curl_close($ch);
        echo $response;
    
    
    
    
    ?>
    

    【讨论】:

    • 感谢@Digant Shah,您救了我的命,并明确了 CRM 的概念,非常感谢!!!!
    【解决方案2】:

    这对我有用。我写了2个方法。一个用于获取授权密钥,一个用于创建联系人。

    <?php 
    class zoho{
    
        public function getAuth()
        {
            $username = "youremail@mail.com";
            $password = "yourpassword";
            $param = "SCOPE=ZohoCRM/crmapi&EMAIL_ID=".$username."&PASSWORD=".$password;
            $ch = curl_init("https://accounts.zoho.com/apiauthtoken/nb/create");
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
            $result = curl_exec($ch);
            /*This part of the code below will separate the Authtoken from the result.
            Remove this part if you just need only the result*/
            $anArray = explode("\n",$result);
            $authToken = explode("=",$anArray['2']);
            $cmp = strcmp($authToken['0'],"AUTHTOKEN");
            echo $anArray['2'].""; if ($cmp == 0)
            {
            echo "Created Authtoken is : ".$authToken['1'];
            return $authToken['1'];
            }
            curl_close($ch);
        }   
    
    
    
    public function postData($auth, $fornavn,$efternavn, $email,$addr,$by,$postnr,$land,$kommentar)
        {
            $xml = 
            '<?xml version="1.0" encoding="UTF-8"?>
            <Contacts>
            <row no="1">
            <FL val="First Name">'.$fornavn.'</FL>
            <FL val="Last Name">'.$efternavn.'</FL>
            <FL val="Email">'.$email.'</FL>
            <FL val="Department">'.$land.'</FL>
            <FL val="Phone">999999999</FL>
            <FL val="Fax">99999999</FL>
            <FL val="Mobile">99989989</FL>
            <FL val="Assistant">none</FL>
            </row>
            </Contacts>';
    
        $url ="https://crm.zoho.com/crm/private/xml/Contacts/insertRecords";
        $query="authtoken=".$auth."&scope=crmapi&newFormat=1&xmlData=".$xml;
        $ch = curl_init();
        /* set url to send post request */
        curl_setopt($ch, CURLOPT_URL, $url);
        /* allow redirects */
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        /* return a response into a variable */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        /* times out after 30s */
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        /* set POST method */
        curl_setopt($ch, CURLOPT_POST, 1);
        /* add POST fields parameters */
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query);// Set the request as a POST FIELD for curl.
    
        //Execute cUrl session
        $response = curl_exec($ch);
        curl_close($ch);
        echo $response;
    
        }
    }
    
    ?>
    

    这样使用:

    <?php 
        include('zoho.php');
        $zoho = new zoho();
        echo "testing....<br />";
        $auth = $zoho->getAuth();
        echo " <pre>";
        echo $auth;
        $result = $zoho->postData($auth, 'Bob','test', 'lol@lol.dk','adresse','by','postr','Danmark','Some comment');
        print_r($result);
     ?>
    

    我仍在研究需要在联系人上插入地址信息的 XML 类型。

    【讨论】:

    • 现在我知道这是个坏主意。不要在每个请求上都请求 authtoken。相反,如果您当前的密钥不起作用,则在其中硬编码密钥,或者甚至更好地创建一个请求新密钥的方法。
    • 现在有一个 newer way 在 2020 年处理这个问题。
    【解决方案3】:
    `$ch = curl_init('https://crm.zoho.com/crm/private/xml/Contacts/insertRecords?');
    curl_setopt($ch, CURLOPT_VERBOSE, 1);//standard i/o streams 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set to return data to string ($response) 
    curl_setopt($ch, CURLOPT_POST, 1);//Regular post 
    

    【讨论】:

    • $authtoken = "7d3e4882977bb4ec00b457cff3292196"; $xmlData='ScottJamestest@test.comCG9999999999999999999989989 John';$query = "newFormat=1&authtoken={$authtoken}&scope=crmapi&xmlData ={$xmlData}"; curl_setopt($ch, CURLOPT_POSTFIELDS, $query); $response = curl_exec($ch);回声$响应; curl_close($ch);
    【解决方案4】:

    如果您在使用 SSL 连接时遇到问题,请尝试添加下一个 curl 选项:

    curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'rsa_rc4_128_sha');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-01
      • 2014-04-04
      • 2019-11-04
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      相关资源
      最近更新 更多