首页 文章

使用 PHP 通过 API 将记录插入 Zoho CRM

提问于
浏览
1

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

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

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

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 运行时没有任何错误。有谁知道我需要做些什么才能让它发挥作用?

4 回答

  • 3

    这对我有用。我写了两个方法。一个用于获取身份验证密钥,另一个用于创建联系人。

    <?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....
    "; $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。

  • 2
    <?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;
    
    ?>
    
  • 1

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

    curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'rsa_rc4_128_sha');
    
  • 0
    `$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
    

相关问题